How to run a process in the background without keeping a batch file open?

9

6

I have a Windows service (the Bamboo integration server) that runs a batch file as a subprocess (a build job) of that script. Within that batch file I would like to be able to start a process (let's call it workerprocess.exe) and have that process run in the background. This is all good, I've used:

start "title" /B workerprocess.exe

That is all well and good. The problem is that this then holds up the completion of the build job. So the batch script finishes, but because of the workerprocess.exe subprocess, the service (Bamboo) does not know that it has finished: it still waits for (and displays output from) the workerprocess.exe.

I've looked in the documentation for the start command, and I can't see anything that does what I want. I saw this question but it didn't really help either - the service still ends up waiting for the process to finish.

So I guess in summary: how can I run a new process from a batch script so that it is completely detached and won't hold up anything that happens to be waiting for that batch script to complete.

Jack Scott

Posted 2013-09-05T02:45:20.573

Reputation: 203

1Have you tried it without the "/B" parameter so that it doesn't start the process within the same command window? – Josh – 2013-09-05T02:52:55.253

@JoshR, that changed things. The service no longer receives output from the process, but is still blocked by it. – Jack Scott – 2013-09-05T03:02:37.973

Answers

2

Have you tried Hidden Start (HSTART)? (Costs $20)

I use it personally to run an hourly batch job with the window hidden. They also mention that you can run commands sequentially as a parameter (or by default, I assume) run asynchronously. I don't know how this will affect your contention on CPU, memory, or disk... but the software also gives you the option to wait some time before performing the action.

Sun

Posted 2013-09-05T02:45:20.573

Reputation: 5 198

When I wrote my answer in Sep 2013, the product was free all around. Sadly they are charging a price. – Sun – 2017-04-17T18:21:20.773

3

All linked scripts can be downloaded and saved with whatever name you find convenient.

1) The IEXPRESS solution - as arguments accepts only the command and its arguments.

Example usage:

call hidder.bat myBat.bat  myexe.exe
call myexe.exe

2) SCHTASKS - Again accepts only two arguments - the command and the arguments.Also checks if it's started with elevated permissions and if possible gets the PID of the process with WEVTUTIL command.

Example usage:

call SCHPhidden.bat "cmd /c myBat.bat"  "argument"

3) 'WScript.Shell' - the script is full wrapper of 'WScript.Shell' and every possible option can be set through the command line options.It's a jscript/batch hybrid and can be called as a bat.

Example usage (for more info print the help with '-h'):

call ShellRunJS.bat "notepad.exe" -style 0 -wait no 

4) 'Win32_ProcessStartup' - again full wrapper and all options are accessible through the command line arguments.This time it's WSF/batch hybrid with some Jscript and some VBScript pieces of code - but it returns the PID of the started process.If process id not hidden some options like X/Y coordinates can be used.

Example usage (for more info print the help with '-h').This will require the full path to the executable/script if it is not in the %path%:

call win32process.bat "notepad" -arguments "/A openFile.txt"  -showWindow 0 -title "notepad"

5) The .NET solution . Most of the options of ProcessStartInfo options are used (but at the end I was too tired to include everything).Should return the PID but it does not:

Example usage (for more info print the help with '-h'):

call ProcessStartJS.bat "notepad" -arguments "/A openFile.txt"  -style Hidden -directory "." -title "notepad" -priority Normal

npocmaka

Posted 2013-09-05T02:45:20.573

Reputation: 887

The link is not found – Rahil Wazir – 2015-10-14T10:18:51.750

@rahilwazir - edited. – npocmaka – 2015-10-14T10:23:06.410

1I tried these a bit - I think the details need to be cleaned up a GREAT deal. Simply looking at the first approach: IExpress, the link is to hidder.bat the example above uses something else. When I ran the script, it failed out asking me for an EXE which is not mentioned above. That might be ok if there was markdown on github, but there is not. Finally, running it produced some .DDF files, but nothing else.... – JoeG – 2016-06-06T15:18:29.967