29

In Linux you can do this:

$ php blah.php > some.log &

to run blah.php in the background. This is the same as Ctrl+z then the bg command.

Is there an equivalent of either/both for the Windows Command Prompt?

4
  • 1
    I don't believe there is, but you can launch as many command prompts as you need. What are you trying to accomplish?
    – uSlackr
    Aug 27, 2012 at 17:37
  • I'm using > to log to a file, and I don't want to prompt to just sit out there. I may want to continue working on the same prompt window w/ the same directory.
    – DOOManiac
    Aug 27, 2012 at 17:39
  • What version of windows?
    – EBGreen
    Aug 27, 2012 at 17:43
  • Windows 7 (64-bit)
    – DOOManiac
    Aug 27, 2012 at 20:18

4 Answers 4

48

Windows does have a similar functionality to Linux's &, to launch processes such that they don't take over your console. Instead of a command-line flag, though, it's a command prefix.

Simply run your command with start in front of it, as such: C:\> start myprog.exe

It also works with commands, not just executables: C:\> start dir

This will start a new console window and run the command inside it.

If you don't want to have a new console window come up when running the command, use the /B switch, like this: C:\> start /B myprog.exe

There are several other options you can specify to configure how to run the command. You can figure them out by reading the help for start by using start /?.

2
  • 2
    Is there any way to list the background processes and then kill them? Oct 15, 2020 at 22:24
  • @JellicleCat You can use tasklist to get a list of running processes and their IDs. taskkill can then be used to end processes.
    – Danny
    Aug 19, 2022 at 22:00
3

I see some users have suggested using start with the /B option. A problem with this approach is that does not work well with console applications. At least in my experience, the child process always terminates once cmd is closed.

However, the PowerShell command Start-Process -WindowStyle hidden <path to executable> does the job.

1

This can run a file in the background from the command prompt or a batch file

@Echo off
Echo Set WshShell = CreateObject("WScript.Shell")   >>%temp%\ghost.vbs
Echo WshShell.Run chr(34) ^& "MyFile" ^& Chr(34), 0 >>%temp%\ghost.vbs
Echo Set WshShell = Nothing                         >>%temp%\ghost.vbs
start %temp%\ghost.vbs
timeout /t 1 >nul
del %temp%\ghost.vbs

Now replace MyFile with the file you wish to run in the background.

2
  • 2
    (1) Can you explain how this works (just a little)? (2) Fundamentally, this uses the start command.  It creates an intermediate thing that runs the user’s command, and then uses the start command to start the intermediate thing. So it’s a Rube Goldberg machine. How is this any better than the other answer, which suggests using start directly? (3) The question shows I/O redirection (> some.log). How would the user do I/O redirection with this answer? Put it on the command line, or build it into the WshShell.Run line in the script? Oct 4, 2017 at 23:45
  • Do not respond in comments; edit your answer to make it clearer and more complete. Oct 4, 2017 at 23:47
0

Just for fun I tried putting "ssh -N -f -l" into Windows Powershell / command prompt, and it does go into the background, but then you have to kill the window to get rid of it. I would like to have a powershell script that runs "ssh -N -f -l" to establish a port tunnel, but then I wouldn't be able to put any new lines in the script after the ssh line, it just hangs there and executes the remaining lines only if ssh fails. I wish there was a powershell equivalent of Linux's ampersand so I could go to the next line and perhaps run mstsc.exe .

1
  • An interesting tidbit but also not a generalized solution
    – Eli B.
    Sep 14, 2020 at 4:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .