Background processes in batch with redirected output - windows

I'm trying to run several background processes from a batch file and have the output directed to a file. Is it possible to do this in Windows? This is what I've tried but it end up directing the output of the start program rather then background process.
start myapp.exe > myapp.out 2>&1

Actually it is quite easy without using a helper batch file. You just need to run the application via cmd.exe instead, and make sure to escape the special characters so they pass through to cmd.exe.
You probably don't want to see an extra console window, so use the START /B option.
start /b "" cmd /c myapp.exe ^>myapp.out 2^>^&1
Each STARTed process must have its output directed to a unique file. Multiple processes cannot share the same output file.

I think the only chance you have is to create one batch file for each exe that you want to start. Inside the batch file you can redirect the output. The master batch file would then "start" the batch file, not the exe directly.
You just need to include an exit command at the end of each batch file:
start_myapp.cmd contains the following:
myapp.exe > myapp.out 2>&1
exit
then you can run
start start_myapp.cmd
and the output will be redirected

Related

run exe with parameters silently from batch file

I want to execute an executable along with it's parameters from a Batch file silently without printing anything on the console from the executable and the executable shouldn't be executed from another console(it shouldn't open another command-prompt to execute). For that I tried with start command as following and couldn't execute it.
start "C:\myApp.exe -mode='a' -inputFilePath='%filePath%'" -i silent
Where %filePath% is a variable and -mode,-inputFilePath are the arguments to my executable.
Please correct me for anything wrong in above statement and help me out in this. Thanks!
From your batch file, just execute the following:
C:\myApp.exe -mode='a' -inputFilePath='%filePath%' >NUL
the >NUL redirects the output to nothing for that command.

Redirecting output of multiple bat files to one console window

I am running multiple microservices locally for development purposes. Currently I am doing it by a .bat script, where I am calling other .bat files, which are starting my services. It starts multiple console windows, and this is not handy to navigate between outputs.
The script which is starting services now looks like this:
start cmd /k call api_one.bat
start cmd /k call api_two.bat
...
In api_xxx.bat I am starting iisexpress with my api.
I am looking for a way to make it more comfortable. For example by redirecting all of the outputs to a single one, with for example a prefix to identify from which script output lines are from.
I had a similar problem, where I was calling a batch file from a batch file which started another batch file.. to complicated to explain but let me get you some code that helped me:
content from test.bat:
set logname=.\log.log
if exist %logname% ( type nul > %logname%
)
echo Now starting example.bat>>%logname%
call C:\example.bat >>%logname%
echo finished example.bat>>%logname%
so now here is content of example.bat:
echo starting integrated commands
start /b cmd /k "C:\example2.bat"
[some code to check for a process that starts in example2.bat]
echo finished integrated commands
and the content of example2.bat can be whatever you want and is also displayed in the logfile.
Note that my example2.bat has an exit at the end, because example.bat checks if example2.bat is running. only when it finishes it goes on with the script. For myself it worked out I got everything from all 3 scripts into one logfile.

.cmd file on Win10 - How to "multi thread" to have actions fire concurrently? [duplicate]

Say, if I have
foo.exe
bar.exe
baz.exe
How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop?
Using the START command to run each program should get you what you need:
START "title" [/D path] [options] "command" [parameters]
Every START invocation runs the command given in its parameter and returns immediately, unless executed with a /WAIT switch.
That applies to command-line apps. Apps without command line return immediately anyway, so to be sure, if you want to run all asynchronously, use START.
Combining a couple of the previous answers, you could try start /b cmd /c foo.exe.
For a trivial example, if you wanted to print out the versions of java/groovy/grails/gradle, you could do this in a batch file:
#start /b cmd /c java -version
#start /b cmd /c gradle -version
#start /b cmd /c groovy -version
#start /b cmd /c grails -version
If you have something like Process Explorer (Sysinternals), you will see a few child cmd.exe processes each with a java process (as per the above commands). The output will print to the screen in whatever order they finish.
start /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
cmd /c : Carries out the command specified by string and then terminates
You can use the start command to spawn background processes without launching new windows:
start /b foo.exe
The new process will not be interruptable with CTRL-C; you can kill it only with CTRL-BREAK (or by closing the window, or via Task Manager.)
Create a batch file with the following lines:
start foo.exe
start bar.exe
start baz.exe
The start command runs your command in a new window, so all 3 commands would run asynchronously.
Use the START command:
start [programPath]
If the path to the program contains spaces remember to add quotes. In this case you also need to provide a title for the opening console window
start "[title]" "[program path]"
If you need to provide arguments append them at the end (outside the command quotes)
start "[title]" "[program path]" [list of command args]
Use the /b option to avoid opening a new console window (but in that case you cannot interrupt the application using CTRL-C
There's a third (and potentially much easier) option. If you want to spin up multiple instances of a single program, using a Unix-style command processor like Xargs or GNU Parallel can make that a fairly straightforward process.
There's a win32 Xargs clone called PPX2 that makes this fairly straightforward.
For instance, if you wanted to transcode a directory of video files, you could run the command:
dir /b *.mpg |ppx2 -P 4 -I {} -L 1 ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"
Picking this apart, dir /b *.mpg grabs a list of .mpg files in my current directory, the | operator pipes this list into ppx2, which then builds a series of commands to be executed in parallel; 4 at a time, as specified here by the -P 4 operator. The -L 1 operator tells ppx2 to only send one line of our directory listing to ffmpeg at a time.
After that, you just write your command line (ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"), and {} gets automatically substituted for each line of your directory listing.
It's not universally applicable to every case, but is a whole lot easier than using the batch file workarounds detailed above. Of course, if you're not dealing with a list of files, you could also pipe the contents of a textfile or any other program into the input of pxx2.
I could not get anything to work I ended up just using powershell to start bat scripts .. sometimes even start cmd /c does not work not sure why .. I even tried stuff like start cmd /c notepad & exit
start-Process "c:\BACKUP\PRIVATE\MobaXterm_Portable\MobaXterm_Portable.bat" -WindowStyle Hidden

Why is an error message output instead of starting an executable in subdirectory of batch file directory?

An error message is output when I try to start "launcher.exe" located in anylocation\ffa\ with this command:
start \ffa\launcher.exe
Batch script should have access to file because stored on disk like this:
ANYLOCATION/myprogram.bat
ANYLOCATION/ffa/launcher.exe
If I type
start /ffa/launcher.exe
output is: invalid switch
But if I type
start \ffa\launcher.exe
output is system cannot find file
Which mistake to I made on starting launcher.exe?
%~dp0
is your batch file directory. So
%~dp0\ffa\launcher.exe
There is no need to use start.
Starting a Program
See start /? and call /? for help on all three ways.
Specify a program name
c:\windows\notepad.exe
In a batch file the batch will wait for the program to exit. When
typed the command prompt does not wait for graphical
programs to exit.
If the program is a batch file control is transferred and the rest of the calling batch file is not executed.
Use Start command
start "" c:\windows\notepad.exe
Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.
Start uses the Windows graphical shell - same as typing in WinKey + R (Run dialog). Try
start shell:cache
Use Call command
Call is used to start batch files and wait for them to exit and continue the current batch file.

cmd.exe doesn't terminate when using a .bat file

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
If start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat

Resources