Running Tasks Asynchronously In CMD - image

I am working on modifying the script my company uses for imaging pc's. I noticed that in our current script tasks are started like the following example:
#echo off
firststep.bat
secondstep.bat
thirdstep.bat
When running I notice that tasks happen one after another. My idea is to run asynchronously when possible. I looked up several examples of asynchronous process running for CMD and I found two ways that are supposed to run processes asynchronously. They are as follows:
#echo off
REM First Example
start firststep.bat
start secondstep.bat
start thirdstep.bat
and:
#echo off
REM Second Example
start /b firststep.bat
start /b secondstep.bat
start /b thirdstep.bat
I have read that /b signifies you want to run a binary process, but I do not understand fully what the difference between start /b and start is. Which of these is more preferable to be used in this circumstance? Would either of these effectively accomplish my goal of expediting our imaging process or should I be using a different way?
Thanks in advance for all help.

/b runs the program in the same window, without it each process would run in another window which closes when it exits.

Related

Can I call %COMSPEC% without terminating all Windows batch execution?

Came across this oddity of batch behaviour as part of the build process in our systems that I'm trying to automate in Jenkins pipelines.
To skip to the meat of the problem and why I'm encountering it, the batch file I'm calling is a build generated batch file that must be called prior to another executable within the same command window to set up paths and alike for our executable to build our system. When building as a user this is fine, you just open the cmd console, run the batch, then run the executable. However in Jenkins I have to pass all commands effectively as one batch file to make it call within the same window, unless I want to mess around with configuring all those paths by hand using withEnv or something.
Something along the lines of this in my Jenkins groovy script:
// Option 1 - && operator
bat "env_configuration.bat && env_dependent_exec.exe"
// Option 2 - multi line version
bat """env_configuration.bat
env_dependent_exec.exe
"""
// Option 3 - same using calls, yadda yadda
bat """call env_configuration.bat
call env_dependent_exec.exe
"""
// Option 4 - Place these calls in batch file and call that instead
bat "run_it_all.bat"
As part of the batch file however, for whatever reason, it has this command within it.
%COMSPEC%
exit /b 0
This will call "C:\WINDOWS\system32\cmd.exe" and output the Mircrosoft version and related blurb.e.g:
Microsoft Windows [Version 10.0.19045.2486]
(c) Microsoft Corporation. All rights reserved.
The catch is, calling this executable will immediately end all batch execution. The line "exit /b 0" isn't actually hit, something whoever created this process I assume never realised. After this batch file process completes, all subsequent commands/calls, (Option 1 above is the easiest to repro) are never hit as all batch processing just stops. This is visible directly in cmd itself, you don't need Jenkins to reproduce.
I will probably wind up just find and replacing this line to comment it out or something... but I refuse to believe I can't find some way of stopping %COMSPEC% from ending all execution of whatever batch file calls it. If I were to guess, I would guess that cmd.exe calls EXIT as it finishes and kills all....
For the sake of interest I have tried modifying the batch file in numerous ways to try and see if I can call %COMSPEC% and still get the rest of the batch script to run afterwards.
:: This fails the same way
call %COMSPEC%
exit /b 0
:: This succeeds to run, but spawns another cmd window that doens't print in the original calling batch file output, so doesn't achieve the original goal
start %COMSPEC%
exit /b 0
:: call %COMSPEC% via subroutine also immediately terminates, we never see "echo 2"
call :comspec_call
exit /b 0
:comspec_call
#echo %COMSPEC% echo 1
%COMSPEC%
#echo %COMSPEC% echo 2
I would guess cmd.exe calls the EXIT command flat on termination, hence the process death, but I've yet to find a means to prevent it doing so...
I've checked %COMSPEC% for flags that might just printout and terminate nicely, but any flags I've found that do provide this information, also terminate with EXIT (I think)
Does anyone has any idea how to call this line and continue execution as I assume the original dev intended?
Cheers in advance!
Batch processing doesn’t just stop — you have started another command interpreter, and the one that launched it is waiting for the new instance to terminate. As it doesn’t quit, everything appears to halt and you wind up killing both to get things back to normal.
Create a new batch script that does everything:
:: controller.bat
#echo off
call env_configuration.bat
call some_other.bat
env_dependent_exec.exe
...
Use of the call command causes the command shell to invoke a script using the current interpreter.
Now your Jenkins groovy script should be:
bat controller.bat
(Disclaimer: I don’t know Jenkins, so...)
Thanks to #Duthomhas and #Magoo
The problem here is that a call to %COMSPEC% launches a new command line process that must terminate before the rest of the script can continue. I didn't understand that cmd.exe was spawning a new script that the call script was waiting to terminate.
With a simplified recap:
:: env_configuration.bat
#echo env_configuration before COMSPEC
#%COMSPEC%
#echo env_configuration after COMSPEC
:: controller.bat
#echo controller before env_configuration.bat
#call env_configuration.bat
#echo controller after env_configuration.bat
controller.bat && #echo back in shell
When you run controller.bat the process will output the Microsoft blurb, but halt.
If you enter "exit" at this point it will kick out of the terminal launched with %COMSPEC% and then all following script steps continue.
It appears the original dev needed the user to be in a new subprocess to continue. The final solution to doing as was intended here is:
:: env_configuration.bat
#echo env_configuration before COMSPEC
#call %COMSPEC% /C <whatever_command_or_exec_next>
#echo env_configuration after COMSPEC
:: controller.bat
#echo controller before env_configuration.bat
#call env_configuration.bat
#echo controller after env_configuration.bat
controller.bat && #echo back in shell
Cheers!

set batch script for loop to NOT wait for previous iteration to complete

I have the following script which launches multiple sessions of RDP connection files at once (colleted from a folder which only contains rdp files).
If I launch this from cmd prompt, it launches all sessions in parallel (which is what I want)
for /r %i in (*.rdp) do (mstsc %~nxi /f)
**while **if i run this script, it just launches the first session then waits for the relative process to end before running the second connection and so on.
for /r %%i in (*.rdp) do (mstsc %%~nxi /f)
What I'm doing wrong? Shouldn't it be the default behavior of batch to run all commands in parallel?
I've checked this but it doesn't address my exact scenario and it doesn't work anyhow as expected (e.g. START myBatchScript.bat doens't change the "waiting for process" behavior)
EDIT
Added answer based on comments (Thanks to #Compo and #Modi)
The coding solution to replicate the cmd behavior as a batch script is using the "start" command (as below, thanks to #Compo)
#for /r %i in (*.rdp) do #(start "" mstsc "%~nxi" /f)
for the reason why this happens refer to #Mofi comments.

Want to execute two commands in windows batch file one by one

I want to send the spool files to printer from spool folder one by one and after processing each file it will be moved to another folder or delete it.
But When I'm trying to run below batch file it directly moves spool files without sending to printer which is due to batch processing.
for %%f in (C:\WINDOWS\system32\spool\PRINTERS\*.SPL) do (
echo %%~nf
start "" E:\spool\xyz\tp.exe "C:\WINDOWS\system32\spool\PRINTERS\%%~nf.SPL" "HP Printer"
move "C:\WINDOWS\system32\spool\PRINTERS\%%~nf.SPL" d:\%%~nf.txt
)
SO need any alternative option without using windows PowerShell.
Thanks in advance.
It is not because of the batch processing. By default batch waits on the termination of a command before moving on. So leaving out the start should do.
The problem is actually the start command. By default start will execute the program as a new process and won't wait for it to finish. Use start /WAIT instead, the /WAIT option will ask it to wait. But as I said earlier, you don't even need start.
Supposing your program exits when it has completed its task, you should do:
for %%f in ("C:\WINDOWS\system32\spool\PRINTERS\*.SPL") do (
echo %%~nf
E:\spool\xyz\tp.exe "%%~ff" "HP Printer"
move "%%~ff" d:\%%~nf.txt
)
In general that should do. But if the program E:\spool\xyz\tp.exe itself starts some kind of background process and exits without waiting for it to terminate, even start /WAIT won't help. As I don't know the program you're using (E:\spool\xyz\tp.exe) I won't be able to help you in that case.
EDIT: Just a little improvement I made in the code: you don't need to specify the whole path to get the file corresponding to the loop variable, %%~ff will do it for you (see this link for others).

My batch file is closing a program too early

Thank you, for those whom took the time to read my question. I am a gamer and would like to execute a few things. First, I would like to Trigger a batch file when I click a program, how do you do that or is it even possible? Basically, activating a game, triggers the batch file.
NOw for the batch file problem, I want to execute Xpadder when I activate games (this is an mmo) and when I close the game I want Xpadder's process/service to close. Ultimately, it's auto trigger,activate,wait,terminate.
That's kind of the process I want it to go if all can be done.
Batch File:
#echo off
start "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
ECHO Blade.xpadderprofile STARTED
start /w "C:\Program Files (x86)\game\games.exe" games.exe
ECHO games STARTED
taskkill /f /im Xpadder.exe
This actually works but the problem is there are two ".exe" files with mmo's. I'll start the game and it would close Xpadder too early because one launcher starts another launcher/client. Xpadder works for the first launcher but the first launcher closes so the game will start. I hope I am explaining myself clear enough.
Reference link: How to automatically close App_A when I close App_B using batchfile
Essentially, this is the same question I have but it's not very clear. What is the batch code to get Xpadder to stay on until the second launcher/client is closed not the first one?
P.S
The game has to open through the launcher then into the second launcher/client or it will not work.
here is the other clients name and path i think:
C:\Program Files (x86)\game\gamer\bin\gam.exe
How about the use of the PsExec, a small MS ulility? Using it, your batch should work:
cmd /c psexec -d "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
start /w "C:\Program Files (x86)\game\games.exe" games.exe
taskkill /f /im Xpadder.exe
The file psexec.exe must be placed in folder enlisted in the system variable WINDIR or PATH, or, otherwise, you should call it with its full path, eg. *C:\Program Files\Others\pstools.exe".
You can add #echo off, salt, pepper or some green Tabasco if you have mon€y for this :D

Kill batch file in such a way that its children are killed too, in Windows

I need to start an exe from a cmd (wrap the exe so I can supply some command-line options). The problem is that just calling the exe from the cmd does not make the wrapping completely transparent: if the .exe hangs, killing the cmd won't kill the exe. I need it to kill the exe too. Can I do that in plain Windows (from XP up), without adding any dependencies?
In Bash you have exec which replaces the shell process with the supplied command. This is handy for writing wrapper scripts, making the wrapping process completely transparent. I know Windows lacks execve() to make this possible, but I'm only interested in the parent-killing-its-children part.
CLARIFICATION: I'm not looking for ways to kill the exe, I am looking for ways to wrap (start) the exe so that killing it using standard ways (eg. Ctrl+C or from task manager) works. For instance, I could create a lnk file (Windows shortcut) and get this behavior, but I want to do it from a script (for one, lnks only work with absolute paths, I can't deploy that).
Thanks.
Taskkill can be used to match certain criteria. By entering Taskkill/? you get the manual and can read up on how to filter using common properties. I assume that all your children share a common portion in their name. You could use taskkill to math the name with wildcards and close all children that matched that name.
EDIT (taken from the comments section):
As IInspectable points out you can kill all child processes using the /T flag.
EDIT starting from a batch you could use START (reference here) to launch the exe parallel to the batch and have your abort in the batch.
Edit i wrote and tested this mini example:
#echo off
echo starting %1
start %1
echo Any key to kill execution
pause >> NUL
taskkill /IM %1 /t
taskkill /F /IM iexplore.exe
/F to force kill.
Which can kill subprocesses
e.g. https://www.windows-commandline.com/taskkill-kill-process/

Resources