So I've written a .bat file (later on refered to as the 2nd one) to backup files every night and then weekly offsite to ensure the files aren't lost. However I realized that a process may be running and currently editing the files so it'd be best to shut down the process and then start it again once the backup is done.
Now, the main problem is this: The process is another batch file running under cmd.exe. There is also a java.exe command window that is created in conjunction with the cmd.exe, and entering the stop into either the cmd.exe or java.exe windows produces the same and the process will stop their process safely. With that in mind I've tried the following commands (minus the *s),
echo stop | cmd.exe
echo stop | java.exe
but what I find is that the cmd.exe pipe is just sending the command to the command window opened by the 2nd batch file. Meanwhile the java.exe command returns an error in the 2nd batch file on how that the proper parameters weren't used.
That being said my main question is this:
Is it possible to pipe commands to specific cmd windows, if so how?
Related
Here is what is going on. I have created a .Bat file containing start javaw -jar BTG_Producer.jar When I start this manually by double clicking it will show my java program in my system tray and shows the process "javaw.exe *32" in Windows Task Manager. When I run this thru Task Scheduler it starts the process "javaw.exe" with out the *32 and does not show the program in the system tray. Do you guys know what I am doing wrong?
When you launch a process via Task Scheduler, it may have a different environment than when you launch the process from your desktop. To test this, you can create a task that runs cmd with the /k parameter from the Task Scheduler. When you run it, cmd will open, then you can run set >%userprofile%\Desktop\environment1.txt. Then, open a cmd as you normally do, and run set >%userprofile%\Desktop\environment2.txt. Open the two files side by side, and compare.
In this case, I suspect your PATH variables are different, which is why 32-bit java is run in one case, and 64-bit in another.
To fix this, you could explicitly specify the 64-bit javaw.exe in your batch file.
C:\path\to\javaw.exe instead of just javaw.
I prepared one batch file to open calc.exe as a practice. I deleted the batch file accidentally. Now whenever I am closing the calc.exe application, every time it gets open after 10 seconds. Please help me.
in Task manager, kill the process conhost.exe (more details) or Windows command processor(fewer details) wherever you encounter it.
Hope that helped!
Use Process Explorer to see what process calc.exe was started by and prevent that for example by deleting its autorun.
Did you purposely start calculator 10 sec after it's been terminated in batch script? But doesn't seems like your deleted batch script is still running. Open cmd and type in taskkill /im "calc.exe", it should taskkill calculator, if it's not, comment below.
There are many references on Internet claiming that one of differences between a GUI and a console application is that running the GUI application from a batch file does not block its execution, while running the console application does block it.
Few of many references, these are particularly from SO/SE:
How can I get an MFC application to block from the command line?
How to wait for a process to terminate to execute another process in batch file
How do you wait for an exe to complete in batch file?
Run a program in a batch script and wait for it to finish before continuing
Moreover, I myself remember this is/was true.
Yet it does not seem to work this way.
I've tested this on a simple batch file like:
echo Pre
notepad
echo Post
The Post is not printed until I close notepad. Why, when a notepad is clearly a GUI application?
I've tested this on Windows 8, 7, and XP just to rule out a possibility that the behavior has changed in recent versions of Windows. I've tried to disable command extensions as one of possible culprits too.
It has to do with how the application that you launch runs and terminates. Some programs launch another process and then terminate, others continue to run. Calc.exe and Notepad.exe simply run until you close them. Write.exe and any program that launches as a result of a file association (e.g., bitmap, wave file, control panel applet, etc.), actually launch another program and then the process that launched them terminates returning control back to the batch file so it can execute the next line.
Here are some examples:
#echo off
echo Starting Calc.exe
calc.exe
echo Calc was closed by the user
echo Starting Notepad.exe
Notepad.exe
echo Notepad was closed by the user
echo Starting WordPad.exe
write.exe
echo Write launched WordPad and then terminated allowing the batchfile to continue
echo Starting Services.msc
services.msc
echo Windows launched MMC, opened services.msc, then returned control to the batchfile
echo Launching WMP via Chord.wav
c:\windows\media\chord.wav
echo Windows launched WMP, opened Chord.wav, then returned control to the batchfile
The CMD process knows Calc and Notepad are still running because it spawned them itself. The CMD process does not know that the others are still running because the intermediate process terminated.
To observe this, open Process Explorer and view the processes displayed in the hierarchical tree. Calc.exe and Notepad.exe both remain as child processes of the CMD process that ran the batchfile. Write.exe and MMC.exe (services.msc) both become top-level processes, no longer children to the CMD process. WMPlayer.exe remains a child process to svchost.exe, which is how Windows launched it. The CMD process doesn't know they are still running because it didn't launch them, some other Windows process did. So execution continues...
One more example of this is how MSPaint.exe functions. If you run it by using the Windows built-in file association for BMPs, then Windows launches MSPaint.exe and control is immediately returned to the batchfile. However, if you pass the BMP to MSPaint.exe, then the batchfile waits for you to close MSPaint before continuing. (I'm on a dev machine with no BMPs, so create a simple one called C:\MyBitmap.bmp.)
#echo off
C:\MyBitmap.bmp
calc.exe
mspaint.exe C:\MyBitmap.bmp
notepad.exe
Calc.exe will open immediately, Notepad.exe will not open until you close the second instance of MSPaint.exe.
I know you didn't ask about launching Windows processes via their file association, but it just demonstrates how the owning process can change. If the CMD process owns the launched process, it should wait until it terminates to continue execution. If the spawned process hands control off to another process, then the CMD process doesn't know about the grandchild process and it continues on with its execution.
Because it waits for return code.You can use start command to create a separate subprocess:
#echo pre
#start "notepad" notepad
#echo post
I've used Windows since NT 3.1, and I too would have said "cmd.exe does not wait for GUI programs to terminate" when you simply type the name of the program (as opposed to using the START command). Though memory grows dim, I believe it originally worked this way. But today, my statement is true interactively, false for "batch" files. Having been thus reminded, I vaguely think it was intentionally changed as some point, since the naïve batch-writer expects sequential execution, but I can't be sure and I don't know when.
I think the answer lies in this question Difference between Windows and Console application.
I quote from two answers.
Konrad Rudolph answered:
The sole difference is that a console application always spawns a console if it isn't started from one (or the console is actively suppressed on startup). A windows application, on the other hand, does not spawn a console. It can still attach to an existant console or create a new one using AllocConsole.
This makes Windows applications better suited for GUI applications or background applications because you usually don't want to have a terminal window created for those.
oefe answered:
Console and Windows applications behave differently when called interactively from the command prompt:
When you start a console application, the command prompt doesn't return until the console application exits. When you start a windows application, the command returns immediately.
This is not true for batch files; they will always wait until the application exits.
The difference in this bahviour between cmd and batch made you think that it worked before.
I have a set of batch files. Some of them are shortcuts to long running tasks, like an FTP server. I want to launch them in a separate tab, so that I can look at the output later. I've found the -new_console option, but it doesn't seem to work on batch files (while it does work on plain executables) - ftp.bat -new_console will run in the same console.
Is this by design? If so - is there a workaround?
UPD1: As I suspected, cmd /c ftp.bat -new_console did work. Looks like ConEmu doesn't count .bat or .cmd as executables. Though, the question remains the same.
When you type in your prompt "ftp.bat" cmd don't start new process, it reads file and execute it contents internally.
ConEmu process "-new_console" when shell create new process only.
So, if you need to start cmd/bat in new console - use "cmd /c " or alias for example. Also, you may use "-new_console" inside of batch file, when you starting ftp or any other process.
I would like to run three servers on my Windows machine from command prompt. For this I would like to write a batch file to run these opening three different command prompts.
To generate 3 new CMD windows that remain open, use the following command syntax:
start "application1" cmd.exe /k dir *.exe
start "application2" cmd.exe /k dir *.xml
start "application3" cmd.exe /k dir *.bat
Where the command after /k is whatever application command line that you want to launch.
Replace the "application#" with some TITLE for the window, that makes sense for you (otherwise it will just say something like C:\Windows\System32\CMD.exe).
The windows will stay open, but that doesn't mean your application will still be running. Like the Dir examples above, it may end but the window will remain.
The full command syntax for START is found here.
The full command syntax for CMD is found here.
Why batch?
If you can, my best suggestion would be to ditch batch and download cygwin in order to use bash instead.
You will find bash much more flexible, used (which means you are far more likely to get support from others), and supported.
I am a windows user and tried to learn batch some time ago, but it was really really frustrating. bash is so much more user friendly.
This should do what you want
start application1
start application2
start application3
This will start 3 applications which will open with 3 different command prompts, whether or not they will keep running or not will depend on the app.