How to run a command in the background on Windows?
In linux you can use command & to run command on the background, the same will continue after the shell is offline. I was wondering is there something like that for windows…
5 Answers 5
I’m assuming what you want to do is run a command without an interface (possibly automatically?). On windows there are a number of options for what you are looking for:
Best: write your program as a windows service. These will start when no one logs into the server. They let you select the user account (which can be different than your own) and they will restart if they fail. These run all the time so you can automate tasks at specific times or on a regular schedule from within them. For more information on how to write a windows service you can read a tutorial online such as (http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx).
Better: Start the command and hide the window. Assuming the command is a DOS command you can use a VB or C# script for this. See here for more information. An example is:
You are still going to have to start the command manually or write a task to start the command. This is one of the biggest down falls of this strategy.
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.
7 Answers 7
Actually, the following works fine for me and creates new windows:
Combine that with parameters to start , such as /min , as Moshe pointed out if you don’t want the new windows to spawn in front of you.
Two years old, but for completeness.
Standard, inline approach: (i.e. behaviour you’d get when using & in Linux)
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:
If you want the bat to be run on an independent console: (i.e. another window)
If you want the other window to hang around afterwards:
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:
How can I execute a Windows command line in background?
How can I execute a windows command line in the background, without it interacting with the active user?
11 Answers 11
Your question is pretty vague, but there is a post on ServerFault which may contain the information you need. The answer there describes how to run a batch file window hidden:
You could run it silently using a Windows Script file instead. The Run Method allows you running a script in invisible mode. Create a .vbs file like this one
and schedule it. The second argument in this example sets the window style. 0 means «hide the window.»
This is a little late but I just ran across this question while searching for the answer myself and I found this:
which, on Windows, is the closest to the Linux command:
From the console HELP system:
One problem I saw with it is that you have more than one program writing to the console window, it gets a little confusing and jumbled.
To make it not interact with the user, you can redirect the output to a file:
I suspect you mean: Run something in the background and get the command line back immediately with the launched program continuing.
Which is the Unix equivalent of
the above one is pretty closer with its Unix counterpart program &
You can use this (commented!) PowerShell script:
Save it as a .ps1 file. After enabling script execution (see Enabling Scripts in the PowerShell tag wiki), you can pass it one or two strings: the name of the executable and optionally the arguments line. For example:
I confirm that this works on Windows 10.
This is how my PHP internal server goes into background. So technically it should work for all.
A related answer, with 2 examples:
- Sometimes foreground is not desireable, then you run minimized as below:
call START /MIN «my mongod» «%ProgramFiles%\MongoDB\Server\3.4\bin\mongod.exe»
Hope that helps.
If you want the command-line program to run without the user even knowing about it, define it as a Windows Service and it will run on a schedule.
You can see the correct way to do this in this link:
Summarizing, you have to checkbox for ‘Run whether user is logged on or not’. Task user credentials should be enter after pressing ‘Ok’.
I did this in a batch file: by starting the apps and sending them to the background. Not exact to the spec, but it worked and I could see them start.