How to run multiple DOS commands in parallel? - batch-file

How to run multiple dos commands?
I have a for loop, which runs detection of server to detect which server works and is fast. And because there is more servers, I wish not to run all server detections in sequence, but in parallel.

You can execute commands in parallel with start like this:
start "" ping myserver
start "" nslookup myserver
start "" morecommands
They will each start in their own command prompt and allow you to run multiple commands at the same time from one batch file.

I suggest you to see "How do I run a bat file in the background from another bat file?"
Also, good answer (of using start command) was given in "Parallel execution of shell processes" question page here;
But my recommendation is to use PowerShell. I believe it will perfectly suit your needs.

You can execute commands in parallel with start command, like:
start "" ping google.com
But to execute WITHOUT new window, use the /b option, like:
start /b ping google.com -t
start /b ping example.com -t
Moreover, the -t option makes ping repeat infinite times.
Also, try Ctrl + Break if Ctrl + C does not work (but Ctrl+C works for above example).

if you have multiple parameters use the syntax as below. I have a bat file with script as below:
start "dummyTitle" [/options] D:\path\ProgramName.exe Param1 Param2 Param3
start "dummyTitle" [/options] D:\path\ProgramName.exe Param4 Param5 Param6
This will open multiple consoles.

Related

Execute .bat cmd Commands after entering blocking mode

There are few commands which I often have to execute one by one and repeated multiple times for project development as
1) ----Open Command Prompt
2) cd "C:\Program Files\Redis"
3) redis-cli.exe -h 127.0.0.1 -p 6666
4) 127.0.0.1:6666> flushdb
5) 127.0.0.1:6666> select 1
6) 127.0.0.1:6666> flushdb
7) ----Close Command Prompt
So, I thought of creating a batch file like
#echo off
cd "C:\Program Files\Redis"
redis-cli.exe -h 127.0.0.1 -p 6666
flushdb
select 1
flushdb
But the problem is that once the execution enters redis-cli.exe the further commands are blocked. It stays like
127.0.0.1:6666>_
And waiting for commands. . I want to know how can I supply those remaining commands to this redis-cli.exe and execute.
I played with commands like run/cmd/start and others but no use.
I could not suggest a better Title for this question because I didn't know what to call this kind of blocking behavior or even if it is possible. If anyone comes with a better name, please do update so others can be benefited.
You could check redis-cli.exe /? if there is any documentation about how you can invoke it from the command line and have it execute a command sequence at once. Maybe there is some switch needed like /C "mycommand1 & mycommand2 & mycommand3" as known from cmd.exe (I don`t know any details about the Redis CLI).
The "blocking" you observed is because redis-cli opens up a command prompt itself to process user input. Much like a telnet would do or a wmic or diskpart. Typically you can leave those prompts with the exit command. This also works for cmd.exe itself. But not for telnet ;) It is some kind of mystery to me how to quit from that thing...
The "inside redis mode" is recognizable by the 127.0.0.1:6666> prefix instead of the usual C:\your\path\here> prompt presented by a default "cmd.exe".
The term would be "blocking" after "prompting for input" or "reading from standard in".
You were not too far off and provided a decent title, I would say.

Jenkins job stuck after command start

I have a problem in my jenkins job and I isolated into one command. So I created another separate job to try to fix it.
So in this job, called "teste" I only have one single command:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
This opens a separate cmd window running my bat file, which should keep running "forever".
But the problem is when I do it, my jenkins job keeps stuck into a "exit 0" operation that I have no idea from where it came from.
Thats the console:
[EnvInject] - Loading node environment variables.
Building remotely on Machine01 in workspace C:\workspace\teste
[teste] $ cmd /c call C:\...dir\jenkins.bat
C:\workspace\teste>start cmd /k call "C:\Program Files\myDir\myBat.bat"
C:\workspace\teste>exit 0
Then it keep stuck at that point.
Example of myBat.bat content:
echo hi
pause
There's any way to make this call in another window without waiting for its finish?
I solve my problem changing the way I was calling my other .bat, calling it through powershell. But since I was from a bat file, I used the command to send a powershell command, calling my other bat file.
Also, I've added another line changing the jenkins BUILD_ID to a fake one, so it doesn't kill it.
So I changed from this line:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
To this :
set BUILD_ID=dontKillMe
powershell -Command "Start-Process 'C:\Program Files\myDir\myBat.bat'"
I hope it helps someone someday.

changing title of windows start command

When I am using following command every thing goes well
start "myping" ping google.com -n 20
It runs in a window with title myping
But when I run following command It doesn't use my given title
start "mywget" %PATHTOWGET%wget.exe ..options....
The wget command executes well it downloads file But the title of window changes
I need it to check whether my process completed or not. I am using following command to test it
tasklist /V /FI "WindowTitle eq mywget"
After finishing the download I am running remaining part of batch script
There might be other process using same program so I can't use to test all wgets
I figured out one solution... don't know whether correct option or not, but its not changing the title at the end I gave -q --show-progress so that it doesn't change the title and also shows progress
start "mywget" %PATHTOWGET%wget.exe ..options.... -q --show-progress
let me know if any other answer one has

Return/Exit code for specific statements in a Batch file

the batch file is supposed to run numerous scripts sequentially (a script is dependent on its previous script). The problem is that the work will be completed successfully only when all the scripts run properly in order. Even if one script fails to run, we don't get the desired output. Here is the example of Batch script;
cd some\Relevant\Folder
script1.js
PING localhost -n 10 > NUL
script 2.js
PING localhost -n 10 > NUL
java -cp X:\some\Java\JavaDir JavaScript
PING localhost -n 10 > NUL
X:\some\bat\directory\batfile.bat
X:\some\directory\pythonfile.py
Here the .js files run properly. But .bat files do not run somehow and so .py script (which sends the mail also fails to run). However, when the .bat files were not present in the script, the .py script ran successfully (with desired result).
Is there any way that I can get the return code/exit code of specific statements like successful/unsuccessful runs of .js, .bat or .py files?
PS: I saw "echo %ERRORLEVEL%" solution on the Internet, but it just prints the error level only once (until it changes.. for example: Successful run gives the echo output as 0 only once.. if I echo it after another statement, it doesn't print anything. If I echo it after an unsuccessful run, it prints some other number like 9009...), even if I echo it the second time after another command.
I found a workaround to this. All the statements in the script were not running because even if one statement threw an error, the rest of the statements would not be executed. The workaround was the invoke a new shell of cmd for every script using,
cmd /c absolute\path\script2.js

How to create a .bat file that starts a customized CMD, in which it run a script?

I want to make a automated program in .bat. The program needs to run a command. However, the command must be run from a custom CMD.
If I open a regular CMD, the commands that I will do:
C:\Hardware\bin\StartCustomCMD.bat init (This is the first thing I will type. It starts the custom CMD.)
bb autobuild (This is the second thing that I will type. The command goes into the custom CMD)
You can probably tell that I did not write these scripts. I am trying to set this up in Windows Scheduler so that the script gets run automatically every day. Any help on how can I do that?
Thanks.
Make yourself a new batch file, and embed the other things into it, and then run that.
#echo off
call C:\Hardware\bin\StartCustomCMD.bat
bb autobuild
If bb it itself a batch file, then use call on it also. What call does is execute another batch file, and then continue processing. If you don't use call, when you run one batch file from another, the latter 'takes over' and the caller does not continue.
To do this you can use the timeout and goto command. Timeout waits a period of time in seconds but it can be skipped by pressing any key while cmd is open at the top layer. If you can see cmd press its icon on the desktop until you can not see it. Then using the goto command you can go to the top line. So here would be your script:
:start
C:\Hardware\bin\StartCustomCMD.bat init
bb autobuild
timeout 86400
goto start
You already know what the first two commands do, but timeout 86400 waits exactly one day then the goto start command goes to the first command so that gets repeated. If you need to add any more commands then put them above the timeout 86400 command.

Resources