Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a bat file in the background from another bat file?

I have a "setup" script which I run in the morning which starts all the programs that I need. Now some of those need additional setup of the environment, so I need to wrap them in small BAT scripts.

How do I run such a script on Windows XP in the background?

CALL env-script.bat runs it synchronously, i.e. the setup script can continue only after the command in the env-script has terminated.

START/B env-script.bat runs another instance of CMD.exe in the same command prompt, leaving it in a really messy state (I see the output of the nested CMD.exe, keyboard is dead for a while, script is not executed).

START/B CMD env-script.bat yields the same result. None of the flags in CMD seem to match my bill.

like image 233
Aaron Digulla Avatar asked Mar 16 '09 08:03

Aaron Digulla


People also ask

How do I run a Command Prompt in the background?

If you want to run additional commands while a previous command runs, you can run a command in the background. If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id.

How do I run a batch file minimized?

Run batch file minimized from Windows Task Scheduler But if you want to run it minimized, then you have to make adjustments in the Actions tab. “^& exit” at the end is necessary to close the CMD window after the batch file is executed.

What does @echo off do in bat?

@echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.


1 Answers

Two years old, but for completeness...

Standard, inline approach: (i.e. behaviour you'd get when using & in Linux)

START /B CMD /C CALL "foo.bat" [args [...]] 

Notes: 1. CALL is paired with the .bat file because that where it usually goes.. (i.e. This is just an extension to the CMD /C CALL "foo.bat" form to make it asynchronous. Usually, it's required to correctly get exit codes, but that's a non-issue here.); 2. Double quotes around the .bat file is only needed if the name contains spaces. (The name could be a path in which case there's more likelihood of that.).

If you don't want the output:

START /B CMD /C CALL "foo.bat" [args [...]] >NUL 2>&1 

If you want the bat to be run on an independent console: (i.e. another window)

START CMD /C CALL "foo.bat" [args [...]] 

If you want the other window to hang around afterwards:

START CMD /K CALL "foo.bat" [args [...]] 

Note: This is actually poor form unless you have users that specifically want to use the opened window as a normal console. If you just want the window to stick around in order to see the output, it's better off putting a PAUSE at the end of the bat file. Or even yet, add ^& PAUSE after the command line:

START CMD /C CALL "foo.bat" [args [...]] ^& PAUSE 
like image 120
antak Avatar answered Sep 20 '22 12:09

antak