How can I execute a Windows command line in background?

174

58

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

Mohammad AL-Rawabdeh

Posted 2010-10-12T06:09:11.153

Reputation: 1 941

Duplicates this question on ServerFault.

– Dan Dascalescu – 2015-08-16T20:11:47.247

1Can 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? – omnibrain – 2010-10-12T06:20:04.720

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 – 2010-10-12T06:41:18.887

Answers

40

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."

nhinkle

Posted 2010-10-12T06:09:11.153

Reputation: 35 057

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 – 2016-01-31T00:00:56.920

257

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

Posted 2010-10-12T06:09:11.153

Reputation: 2 671

3This 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 – 2015-06-24T04:56:18.433

1@rogerdpack That's right. For some reason with Windows 7, this is the command format. The "" is the mandatory title parameter. – ejbytes – 2016-01-30T23:58:59.597

@ejbytes I run Windows 7 and I don't need the empty parameter "" in order for it to work. – HelloGoodbye – 2016-04-13T15:26:44.347

1@Novicaine: How do I abort a process I have started that way? Ctrl+c doesn't have any effect on in. – HelloGoodbye – 2016-04-13T15:28:31.373

11Unfortunately, if I exit the shell window in which I spawned the process, it looks like the process also terminates. – palswim – 2016-07-21T22:36:44.153

1How do you enable ^C processing for such application? – Qwerty – 2016-09-06T10:11:02.613

2@Qwerty: Look at the MSDN for SetConsoleCtrlHandler – Zan Lynx – 2016-09-12T19:29:06.137

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 – 2019-07-12T08:48:36.447

@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 – 2020-02-12T03:56:51.673

3I like this answer best because it doesn't open another command window – wisbucky – 2014-01-03T16:17:13.610

70

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 &

Paul Douglas

Posted 2010-10-12T06:09:11.153

Reputation: 701

7what is the fg equivalent? Can we close the command prompt and the porgram will still run? – Nenotlep – 2014-10-15T11:34:42.267

1Also, 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. – Nenotlep – 2014-10-15T11:36:24.247

4What'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). – Dawid Ferenczy Rogožan – 2015-10-14T17:41:32.860

@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 – 2016-04-13T15:23:31.973

@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 – 2016-04-13T15:25:04.667

@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. – Dawid Ferenczy Rogožan – 2016-04-14T00:05:25.740

@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 – 2016-04-14T19:42:32.487

@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 – 2016-04-23T20:10:57.367

15

START /MIN program 

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

Siva Sankaran

Posted 2010-10-12T06:09:11.153

Reputation: 273

7

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.

Ben N

Posted 2010-10-12T06:09:11.153

Reputation: 32 973

4yep start /b no longer works. – Sajuuk – 2018-05-06T05:32:26.360

3

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

Posted 2010-10-12T06:09:11.153

Reputation: 236

2

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.

Manohar Reddy Poreddy

Posted 2010-10-12T06:09:11.153

Reputation: 231

This doesn't seem to run it minimized: call Start /MIN "c" "calc.exe" – moondra – 2018-03-12T04:19:45.640

1correct, it works for notepad: call start /min "n" "notepad.exe" – Manohar Reddy Poreddy – 2018-03-12T06:44:46.203

So it works for windowed applications but not for console applications. Figures, as one can pass the SW_* to CreateProcessW via STARTUPINFO::wShowWindow (including SW_HIDE).

– 0xC0000022L – 2018-08-14T18:57:03.213

What is a full command, with above like "start" or other tool? do we need to write another program? – Manohar Reddy Poreddy – 2018-08-15T01:50:09.137

1

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

Posted 2010-10-12T06:09:11.153

Reputation: 8 576

4how do you do that? – barlop – 2011-09-30T19:26:10.613

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 – 2012-04-06T19:02:22.193

-1

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'.

Daniel Alonso

Posted 2010-10-12T06:09:11.153

Reputation: 11

-2

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

Posted 2010-10-12T06:09:11.153

Reputation: 1