Search code examples
windowscmdbackground-process

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…


Solution

  • 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:

      Set objShell = WScript.CreateObject("WScript.Shell")
      objShell.Run("C:\yourbatch.bat"), 0, True
      

      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.

    • Worst: Start the command using the startup folder. This runs when a user logs into the computer

    Hope that helps some!