- Running Windows batch file commands asynchronously
- 7 Answers 7
- Command to run a .bat file
- 4 Answers 4
- 1. RUN the batch file with full path
- 2. CALL the batch file with full path
- 3. Change directory and RUN batch file with one command line
- 4. Change directory and CALL batch file with one command line
- 5. Change directory and CALL batch file with keeping current environment with one command line
- Windows batch script launch program and exit console
- 6 Answers 6
- Run batch file as a Windows service
- 8 Answers 8
Running Windows batch file commands asynchronously
How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop?
7 Answers 7
Using the START command to run each program should get you what you need:
Every START invocation runs the command given in its parameter and returns immediately, unless executed with a /WAIT switch.
That applies to command-line apps. Apps without command line return immediately anyway, so to be sure, if you want to run all asynchronously, use START .
30 copy FILA DESTA and I wan them to be async, but I also want to have some idea when they’re all done. – Doug Jul 17 ’17 at 9:36
Combining a couple of the previous answers, you could try start /b cmd /c foo.exe .
For a trivial example, if you wanted to print out the versions of java/groovy/grails/gradle, you could do this in a batch file:
If you have something like Process Explorer (Sysinternals), you will see a few child cmd.exe processes each with a java process (as per the above commands). The output will print to the screen in whatever order they finish.
You can use the start command to spawn background processes without launching new windows:
The new process will not be interruptable with CTRL-C; you can kill it only with CTRL-BREAK (or by closing the window, or via Task Manager.)
Create a batch file with the following lines:
The start command runs your command in a new window, so all 3 commands would run asynchronously.
If the path to the program contains spaces remember to add quotes. In this case you also need to provide a title for the opening console window
If you need to provide arguments append them at the end (outside the command quotes)
Use the /b option to avoid opening a new console window (but in that case you cannot interrupt the application using CTRL-C
There’s a third (and potentially much easier) option. If you want to spin up multiple instances of a single program, using a Unix-style command processor like Xargs or GNU Parallel can make that a fairly straightforward process.
There’s a win32 Xargs clone called PPX2 that makes this fairly straightforward.
For instance, if you wanted to transcode a directory of video files, you could run the command:
Picking this apart, dir /b *.mpg grabs a list of .mpg files in my current directory, the | operator pipes this list into ppx2, which then builds a series of commands to be executed in parallel; 4 at a time, as specified here by the -P 4 operator. The -L 1 operator tells ppx2 to only send one line of our directory listing to ffmpeg at a time.
After that, you just write your command line ( ffmpeg.exe -i «<>» -quality:v 1 «<>.mp4″ ), and <> gets automatically substituted for each line of your directory listing.
It’s not universally applicable to every case, but is a whole lot easier than using the batch file workarounds detailed above. Of course, if you’re not dealing with a list of files, you could also pipe the contents of a textfile or any other program into the input of pxx2.
Command to run a .bat file
I’m trying to make my Visual Studio build script execute a .bat file that does something important.
Here is what I’m want to do right now:
But it doesn’t work.
I have to do this to make it work:
But this is pretty difficult to add to the Visual Studio script.
How can I do this in one single line?
4 Answers 4
«F:\- Big Packets -\kitterengine\Common\Template.bat» maybe prefaced with call (see call /? ). Or Cd /d «F:\- Big Packets -\kitterengine\Common\» & Template.bat .
CMD Cheat Sheet
CMD.exe
First thing to remember its a way of operating a computer. It’s the way we did it before WIMP (Windows, Icons, Mouse, Popup menus) became common. It owes it roots to CPM, VMS, and Unix. It was used to start programs and copy and delete files. Also you could change the time and date.
For help on starting CMD type cmd /? . You must start it with either the /k or /c switch unless you just want to type in it.
Getting Help
For general help. Type Help in the command prompt. For each command listed type help (eg help dir ) or /? (eg dir /? ).
Some commands have sub commands. For example schtasks /create /? .
The NET command’s help is unusual. Typing net use /? is brief help. Type net help use for full help. The same applies at the root — net /? is also brief help, use net help .
References in Help to new behaviour are describing changes from CMD in OS/2 and Windows NT4 to the current CMD which is in Windows 2000 and later.
WMIC is a multipurpose command. Type wmic /? .
Punctuation
Naming Files
Starting a Program
See start /? and call /? for help on all three ways.
There are two types of Windows programs — console or non console (these are called GUI even if they don’t have one). Console programs attach to the current console or Windows creates a new console. GUI programs have to explicitly create their own windows.
If a full path isn’t given then Windows looks in
The directory from which the application loaded.
The current directory for the parent process.
Windows NT/2000/XP: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.
Windows NT/2000/XP: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
Specify a program name
This is the standard way to start a program.
In a batch file the batch will wait for the program to exit. When typed the command prompt does not wait for graphical programs to exit.
If the program is a batch file control is transferred and the rest of the calling batch file is not executed.
Use Start command
Start starts programs in non standard ways.
Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.
Start uses the Windows graphical shell — same as typing in WinKey + R (Run dialog). Try
Also program names registered under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths can also be typed without specifying a full path.
Also note the first set of quotes, if any, MUST be the window title.
Use Call command
Call is used to start batch files and wait for them to exit and continue the current batch file.
Other Filenames
Typing a non program filename is the same as double clicking the file.
Keys
Ctrl + C exits a program without exiting the console window.
For other editing keys type Doskey /? .
↑ and ↓ recall commands
ESC clears command line
F7 displays command history
ALT + F7 clears command history
F8 searches command history
F9 selects a command by number
ALT + F10 clears macro definitions
Also not listed
Ctrl + ← or → Moves a word at a time
Ctrl + Backspace Deletes the previous word
Home Beginning of line
End End of line
Ctrl + End Deletes to end of line
There are many possibilities to solve this task.
1. RUN the batch file with full path
The easiest solution is running the batch file with full path.
Once end of batch file Template.bat is reached, there is no return to previous script in case of the command line above is within a *.bat or *.cmd file.
The current directory for the batch file Template.bat is the current directory of the current process. In case of Template.bat requires that the directory of this batch file is the current directory, the batch file Template.bat should contain after @echo off as second line the following command line:
Run in a command prompt window cd /? for getting displayed the help of this command explaining parameter /D . change to specified directory also on a different drive.
Run in a command prompt window call /? for getting displayed the help of this command used also in 2., 4. and 5. solution and explaining also %
dp0 . drive and path of argument 0 which is the name of the batch file.
2. CALL the batch file with full path
Another solution is calling the batch file with full path.
The difference to first solution is that after end of batch file Template.bat is reached the batch processing continues in batch script containing this command line.
For the current directory read above.
3. Change directory and RUN batch file with one command line
There are 3 operators for running multiple commands on one command line: & , && and || .
For details see answer on Single line with multiple commands using Windows batch file
I suggest for this task the && operator.
As on first solution there is no return to current script if this is a *.bat or *.cmd file and changing the directory and continuation of batch processing on Template.bat is successful.
4. Change directory and CALL batch file with one command line
This command line changes the directory and on success calls the batch file.
The difference to third solution is the return to current batch script on exiting processing of Template.bat .
5. Change directory and CALL batch file with keeping current environment with one command line
The four solutions above change the current directory and it is unknown what Template.bat does regarding
- current directory
- environment variables
- command extensions state
- delayed expansion state
In case of it is important to keep the environment of current *.bat or *.cmd script unmodified by whatever Template.bat changes on environment for itself, it is advisable to use setlocal and endlocal .
Run in a command prompt window setlocal /? and endlocal /? for getting displayed the help of these two commands. And read answer on change directory command cd ..not working in batch file after npm install explaining more detailed what these two commands do.
Now there is only & instead of && used as it is important here that after setlocal is executed the command endlocal is finally also executed.
Windows batch script launch program and exit console
I have a batch script that I use to launch a program, such as notepad.exe . When I double click on this batch file, notepad starts normally, but the black window of the cmd who launched notepad.exe remains in the background. What do I have to do in order to launch notepad.exe and make the cmd window disappear?
edit: is more complicated than using \I .
The cmd calls cygwin , and cygwin starts notepad . I use
start \I \path\cygwin\bin\bash.exe
and the first window (cmd) disappears, but a second window (\cygwin\bin\bash.exe) is still on the background. In the cygwin script I used notepad.exe & and then exit.
6 Answers 6
Keep the «» in between start and your application path.
Added explanation:
Normally when we launch a program from a batch file like below, we’ll have the black windows at the background like OP said.
This was cause by Notepad running in same command prompt (process). The command prompt will close AFTER notepad is closed. To avoid that, we can use the start command to start a separate process like this.
This command is fine as long it doesn’t has space in the path. To handle space in the path for just in case, we added the » quotes like this.
However running this command would just start another blank command prompt. Why? If you lookup to the start /? , the start command will recognize the argument between the » as the title of the new command prompt it is going to launch. So, to solve that, we have the command like this:
Run batch file as a Windows service
In order to run one application, a batch file has to be kicked off (which does things like start Jetty, display live logs, etc). The application will work only if this batch file is running. I am hence forced to have this batch file running and not logout from the Windows server.
Can this batch file be run as a service? I am experimenting with one of the suggestions from a similar question.
8 Answers 8
NSSM is totally free and hyper-easy, running command prompt / terminal as administrator:
then a dialog will appear so you can choose where is the file you want to run.
Why not simply set it up as a Scheduled Task that is scheduled to run at start up?
There’s a built in windows cmd to do this: sc create. Not as fancy as nssm, but you don’t have to download an additional piece of software.
- start=demand means you must start the service yourself
- whitespace is required after =
- I did encounter an error on service start that the service did not respond in a timely manner, but it was clear the service had run the .bat successfully. Haven’t dug into this yet but this thread experienced the same thing and solved it using nssm to install the service.
No need for extra software. Use the task scheduler -> create task -> hidden. The checkbox for hidden is in the bottom left corner. Set the task to trigger on login (or whatever condition you like) and choose the task in the actions tab. Running it hidden ensures that the task runs silently in the background like a service.
Note that you must also set the program to run «whether the user is logged in or not» or the program will still run in the foreground.
On Windows 2019 Server, you can run a Minecraft java server with these commands:
sc create minecraft-server DisplayName= «minecraft-server» binpath= «cmd.exe /C C:\Users\Administrator\Desktop\rungui1151.lnk» type= own start= auto
The .lnk file is a standard windows shortcut to a batch file.
— .bat file begins —
java -Xmx40960M -Xms40960M -d64 -jar minecraft_server.1.15.1.jar
All this because:
service does not know how to start in a folder,
cmd.exe does not know how to start in a folder
Starting the service will produce «timely manner» error, but the log file reveals the server is running.
If you need to shut down the server, just go into task manager and find the server java in background processes and end it, or terminate the server from in the game using the /stop command, or for other programs/servers, use the methods relevant to the server.
As Doug Currie says use RunAsService.
From my past experience you must remember that the Service you generate will
- have a completely different set of environment variables
- have to be carefully inspected for rights/permissions issues
- might cause havoc if it opens dialogs asking for any kind of input
not sure if the last one still applies . it was one big night mare in a project I worked on some time ago.
While it is not free (but $39), FireDaemon has worked so well for me I have to recommend it. It will run your batch file but has loads of additional and very useful functionality such as scheduling, service up monitoring, GUI or XML based install of services, dependencies, environmental variables and log management.
I started out using FireDaemon to launch JBoss application servers (run.bat) but shortly after realized that the richness of the FireDaemon configuration abilities allowed me to ditch the batch file and recreate the intent of its commands in the FireDaemon service definition.
There’s also a SUPER FireDaemon called Trinity which you might want to look at if you have a large number of Windows servers on which to manage this service (or technically, any service).