255

How can I execute a windows command line in the background, without it interacting with the active user?

slhck
  • 223,558
  • 70
  • 607
  • 592
Mohammad AL-Rawabdeh
  • 2,811
  • 4
  • 19
  • 9
  • 1
    Can you specify what you want to do? Do you want to perform a command on the command line in background or do you want to perform the whole command line in background, so it is unvisible from the desktop? – oldwired Oct 12 '10 at 06:20
  • i need two cane perform a command on the command line in background or do you want to perform the whole command line in background – Mohammad AL-Rawabdeh Oct 12 '10 at 06:41
  • Duplicates [this question on ServerFault](http://serverfault.com/questions/121979/tools-to-run-a-background-process-command-line-in-windows/714560). – Dan Dascalescu Aug 16 '15 at 20:11

12 Answers12

370

This is a little late but I just ran across this question while searching for the answer myself and I found this:

START /B program

which, on Windows, is the closest to the Linux command:

program &

From the console HELP system:

C:\>HELP START

Starts a separate window to run a specified program or command.

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.
    path        Starting directory.
    B           Start application without creating a new window. The
                application has ^C handling ignored. Unless the application
                enables ^C processing, ^Break is the only way to interrupt
                the application.

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:

START /B program > somefile.txt
Novicaine
  • 3,801
  • 2
  • 12
  • 2
  • 5
    I like this answer best because it doesn't open another command window – wisbucky Jan 03 '14 at 16:17
  • 11
    This doesn't seem to work for me, it seems to only create a new cmd instance [?] however if I run it like `start /B "" program` then it worked... – rogerdpack Jun 24 '15 at 04:56
  • 4
    @rogerdpack That's right. For some reason with Windows 7, this is the command format. The "" is the mandatory title parameter. – ejbytes Jan 30 '16 at 23:58
  • 1
    @ejbytes I run Windows 7 and I don't need the empty parameter `""` in order for it to work. – HelloGoodbye Apr 13 '16 at 15:26
  • 1
    @Novicaine: How do I abort a process I have started that way? Ctrl+c doesn't have any effect on in. – HelloGoodbye Apr 13 '16 at 15:28
  • 20
    Unfortunately, if I exit the shell window in which I spawned the process, it looks like the process also terminates. – palswim Jul 21 '16 at 22:36
  • 1
    How do you enable `^C` processing for such application? – Qwerty Sep 06 '16 at 10:11
  • 2
    @Qwerty: Look at the MSDN for SetConsoleCtrlHandler – Zan Lynx Sep 12 '16 at 19:29
  • This is cool, but is there a way to specify WHICH `start` to run? I have unfortunately a `C:\MinGW\msys\1.0\bin\start` that wants to run instead (I found it with `where start`. It seems `START` here is a built-in command, but windows only wants to run the MinGW one. – Fuhrmanator Jul 12 '19 at 08:48
  • @Novicaine: either open Task Manager (`ctrl`+`shift`+`esc`) and find the process on the list, or use `taskkill.exe /im nameofprocess.exe /f`. I believe you also have the choice of specifying a PID with `taskkill.exe` but I'm not sure – airstrike Feb 12 '20 at 03:56
  • @palswim, I don't know what Windows edition you refer to, but in Windows 10 I tried `start /B %WinDir%\System32\Taskmgr.exe`, and closing the command-line window did not close Task Manager. – Henke Nov 23 '20 at 15:25
94

I suspect you mean: Run something in the background and get the command line back immediately with the launched program continuing.

START "" program

Which is the Unix equivalent of

program &
slhck
  • 223,558
  • 70
  • 607
  • 592
Paul Douglas
  • 941
  • 6
  • 3
  • 10
    what is the fg equivalent? Can we close the command prompt and the porgram will still run? – Joel Peltonen Oct 15 '14 at 11:34
  • 1
    Also, I want to run a program in command prompt and return to it from time to time, like in screen - is that doable with this? I need to be able to close the command prompt but keep the running program usable. – Joel Peltonen Oct 15 '14 at 11:36
  • 6
    What's that empty parameter of `start`? It doesn't work without it (executes just a new command instance), but `start`'s help doesn't say anything about it, it states all parameters are optinional (or I don't understand it). – David Ferenczy Rogožan Oct 14 '15 at 17:41
  • @DawidFerenczy `start` works without the empty parameter for me, but I seem to get a shells with a separate configuration when I use the empty parameter, as a setting I did when I didn't have the empty parameter isn't used when I do use the empty parameter. I wonder why they use separate configurations? – HelloGoodbye Apr 13 '16 at 15:23
  • 1
    @Paul `START "" program` starts a command in a new terminal for me, while `program &` in Unix runs the command in and prints the output to the same terminal. – HelloGoodbye Apr 13 '16 at 15:25
  • @HelloGoodbye Sorry, but I don't understand a single sentence. "the empty parameter isn't used when I do use the empty parameter" doesn't make a sense at all. – David Ferenczy Rogožan Apr 14 '16 at 00:05
  • @Dawid, I can see that my sentence was a bit ambiguous. "Isn't used" didn't refer to the empty parameter, but to the setting that I did (changed?) when I didn't use the empty parameter. In both cases (with/without the `""` parameter), I get a new cmd window. The first time, when I called `START` without the `""` parameter, I changed a setting to the cmd. Later when I called `START` _with_ the `""` parameter, the setting had been undone. – HelloGoodbye Apr 14 '16 at 19:42
  • @HelloGoodbye It worked for me with a GUI program, closed the command prompt immediately after starting it. I assume you're trying to start a command-line program in the background. – NobleUplift Apr 23 '16 at 20:10
54

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

Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0
Set WinScriptHost = Nothing

and schedule it. The second argument in this example sets the window style. 0 means "hide the window."

Tobias Kienzler
  • 4,421
  • 7
  • 44
  • 76
nhinkle
  • 37,198
  • 36
  • 140
  • 177
  • This is perfect. SetPoint for Logitech never, NEVER, starts with windows. I've been starting it manually for about 3 years now. Does it matter where the batch is? I've seen some people put this type of batch file in C, or the root. – ejbytes Jan 31 '16 at 00:00
  • 3
    It really isn't vague if you are used to linux. Just put a & at the end and its backgrounded. – Paul Rooney Feb 08 '21 at 07:14
19
START /MIN program 

the above one is pretty closer with its Unix counterpart program &

Siva Sankaran
  • 341
  • 2
  • 8
11

You can use this (commented!) PowerShell script:

# Create the .NET objects
$psi = New-Object System.Diagnostics.ProcessStartInfo
$newproc = New-Object System.Diagnostics.Process
# Basic stuff, process name and arguments
$psi.FileName = $args[0]
$psi.Arguments = $args[1]
# Hide any window it might try to create
$psi.CreateNoWindow = $true
$psi.WindowStyle = 'Hidden'
# Set up and start the process
$newproc.StartInfo = $psi
$newproc.Start()
# Return the process object to the caller
$newproc

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:

.\hideproc.ps1 'sc' 'stop SomeService'

I confirm that this works on Windows 10.

Tobias Kienzler
  • 4,421
  • 7
  • 44
  • 76
Ben N
  • 40,045
  • 17
  • 140
  • 181
6

This is how my PHP internal server goes into background. So technically it should work for all.

start /B "" php -S 0.0.0.0:8000 &

Thanks

Suyash Jain
  • 286
  • 3
  • 9
4

A related answer, with 2 examples:

  1. Below opens calc.exe:

call START /B "my calc" "calc.exe"

  1. Sometimes foreground is not desireable, then you run minimized as below:

call start /min "n" "notepad.exe"

call START /MIN "my mongod" "%ProgramFiles%\MongoDB\Server\3.4\bin\mongod.exe"

Hope that helps.

  • 1
    This doesn't seem to run it minimized: `call Start /MIN "c" "calc.exe"` – moondra Mar 12 '18 at 04:19
  • 1
    correct, it works for notepad: call start /min "n" "notepad.exe" – Manohar Reddy Poreddy Mar 12 '18 at 06:44
  • 1
    So it works for windowed applications but not for console applications. Figures, as one can pass the `SW_*` to [`CreateProcessW`](https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessw) via `STARTUPINFO::wShowWindow` (including `SW_HIDE`). – 0xC0000022L Aug 14 '18 at 18:57
  • What is a full command, with above like "start" or other tool? do we need to write another program? – Manohar Reddy Poreddy Aug 15 '18 at 01:50
  • @0xC0000022L No, you just need to pass Process Creation Flag of `CREATE_NO_WINDOW`, as simple as that. – ScienceDiscoverer Aug 17 '22 at 15:16
  • @ManoharReddyPoreddy You can use [my utility](https://github.com/ScienceDiscoverer/ql) as and example, or as is! – ScienceDiscoverer Aug 17 '22 at 15:17
  • @ScienceDiscoverer your point being? I was referring to how this answer does not work for console applications and how the flags to `start` likely map to the underlying API. If you can tell how to achieve the same with `start`, sure I'll give that a shot. While being a developer, when using NT script I am merely a user of existing facilities. – 0xC0000022L Aug 29 '22 at 07:28
3

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.

CarlF
  • 8,846
  • 3
  • 24
  • 40
  • 4
    how do you do that? – barlop Sep 30 '11 at 19:26
  • 1
    Alternatively you can make it a scheduled task - Control Panel->Administrative Tools->Scheduled Tasks or use the `schtasks` command in Windows XP and above (warning: `schtasks` is complicated). – LawrenceC Apr 06 '12 at 19:02
0

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.

rem   Work Start Batch Job from Desktop
rem   Launchs All Work Apps
@echo off
start "Start OneDrive" "C:\Users\username\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
start  "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe"
start skype
start "Start Teams" "C:\Users\username\AppData\Local\Microsoft\Teams\current\Teams.exe"
start Slack
start Zoom
sleep 10
taskkill /IM "explorer.exe"
taskkill /IM "teams.exe"
taskkill /IM "skype.exe"
taskkill /IM "slack.exe"
taskkill /IM "zoom.exe"
taskkill /IM "cmd.exe"
@echo on

killing explorer kills all explorer windows, I run this batch file after start up, so killing explorer is no issue for me. You can seemingly have multiple explorer processes and kill them individually but I could not get it to work. killing cmd.exe is to close the CMD window which starts because of the bad apps erroring.

  • 2
    You are launching work apps and then terminating them all ? Why ? – Amit Naidu Apr 23 '21 at 22:10
  • 1
    Amit, many of the comments in previous questions asked about how to interact/close running programs you open. This is apparently an illustration of how to do that. – Jeff Clayton Nov 18 '22 at 13:37
0

You can use my utility. I think the source code should be self explanatory. Basically CreateProcess with CREATE_NO_WINDOW flag.

ScienceDiscoverer
  • 322
  • 1
  • 3
  • 11
0

You can see the correct way to do this in this link:

How to Run a Scheduled Task Without a Command Window Appearing

Summarizing, you have to checkbox for 'Run whether user is logged on or not'. Task user credentials should be enter after pressing 'Ok'.

-1

just came across this thread windows 7 , using power shell, runs executable's in the background , exact same as unix filename &

example: start -NoNewWindow filename

help start

NAME Start-Process

SYNTAX Start-Process [-FilePath] [[-ArgumentList] ] [-Credential ] [-WorkingDirectory ] [-LoadUserProfile] [-NoNewWindow] [-PassThru] [-RedirectStandardError ] [-RedirectStandardInput ] [-RedirectStandardOutput ] [-Wait] [-WindowStyle {Normal | Hidden | Minimized | Maximized}] [-UseNewEnvironment] []

Start-Process [-FilePath] <string> [[-ArgumentList] <string[]>] [-WorkingDirectory <string>] [-PassThru] [-Verb
<string>] [-Wait] [-WindowStyle <ProcessWindowStyle> {Normal | Hidden | Minimized | Maximized}]
[<CommonParameters>]

ALIASES saps start

jerry
  • 11
  • I don't have Windows 7 anymore to test this on [Windows 11 here] -- but start.exe is also a command. To make this work, without aliases since 'start' is a different but similar command, call it directly from within PowerShell: Start-Process -NoNewWindow filename – Jeff Clayton Nov 18 '22 at 13:49