17

Anybody know if there is cmd script that does that and/or a command line tool ready for the job?

5
  • Doing so can harm your data! Mar 26, 2012 at 16:27
  • 8
    @Santosh: what do you mean by "can harm your data"? In my specific case the background tasks would process their own independent data sets. No risks of concurrent modifications.
    – Sergio
    Mar 26, 2012 at 16:37
  • 1
    From Win32 API side, I think WaitForMultipleObjects with bWaitAll=TRUE would do the job. It wouldn't be hard to write a program that just forks off programs given in some input file, if there are no satisfying answers. Mar 26, 2012 at 20:38
  • 1
    @grawity: yes, I plan on doing just that if nothing comes up (and post the code here), but let's see if I can avoid reinventing the wheel :)
    – Sergio
    Mar 27, 2012 at 10:42
  • Reinventing the wheel is a good thing if currently existing wheels are square... Mar 27, 2012 at 13:24

6 Answers 6

14

Create the three batch files below. Main.bat launches 1.bat and 2.bat

1.bat and 2.bat write out temporary files for which main.bat checks. While 1.bat and 2.bat are working, main.bat reports back that processing is still occurring. When you hit enter on either 1.bat or 2.bat's open window, the temporary file is deleted, and the program exits. This simulates processing stopping for that .bat file. If you do this for both 1 and 2.bat, main.bat reports to you that processing has completed for these processes. You can make 1.bat and 2.bat do anything you want, so long as you clear the temporary file when you are finished. At this point main.bat can do anything you want it to do as well.

1.bat

echo %time% > 1.tmp
pause
del 1.tmp
exit 

2.bat

echo %time% > 2.tmp
pause
del 2.tmp
exit

main.bat

@echo off
start "1" 1.bat
start "2" 2.bat
    @ping -n 1 127.0.0.1 > nul
:loop
@echo Processing......
if not exist *.tmp goto :next
    @ping -n 5 127.0.0.1 > nul
goto loop
:next
@echo Done Processing!
4
  • Having done this before, I can tell you this approach is not as hard as it seems. Additionally, is easy to maintain - to expand and add logic, and easy to train someone else to maintain it. Usually these things take on lives of their own...
    – RobW
    Mar 29, 2012 at 23:59
  • I'm happy with this solution. Jul 18, 2013 at 5:02
  • Could you expand on the @ping command? I don't understand why you choose to do this and what 127.0.0.1 is.
    – LHo
    Mar 22, 2020 at 22:47
  • Joe, when I wrote that response back in 2012, either timeout.exe was not available, or i didnt know about it. In any event, each ping packet takes ~1 second to process. The ping command above pauses the script for ~5 seconds, and >nul tells it to echo nothing to the screen.
    – RobW
    May 17, 2020 at 22:41
6

Simple enough, provided the processes don't need standard input or output. From the command line, or in a batch file:

process1 | process2 | process3 | process4

This will run all four processes simultaneously, and won't return until they've all exited. (Possible exception: if a process explicitly closes the standard output it might be treated as if it had exited. But that's unusual, very few processes do that.)

I'm not sure exactly how many processes you can launch simultaneously this way. I've tried up to ten.

2
  • Uhm, what I get is that all processes are indeed started at once, but process3 is kept waiting till process4 has completed, process2 is kept waiting till process3 completes, and so on. This is from the classic Windows command prompt by the way.
    – Sergio
    Mar 27, 2012 at 9:42
  • @Sergio: What do you mean by "kept waiting"? Mar 27, 2012 at 23:33
1

The only way to do this is to start separate batch files (see start /?) that run the individual task and write a text file upon completion. Then, you periodically check whether the text files have been made.

1

I was also looking for similar task. Objective was to run multiple commands parallel and extract output (stdout & error) of all parallel processes. Then wait for all parallel processes to finish and execute another command. Following is a sample code for BAT file, can be executed in CMD:

( start "" /B cmd /c ping localhost -n 6 ^>nul timeout /t 5 /nobreak start "" /B /D "C:\users\username\Desktop" cmd /c dir ^> dr.txt ^2^>^&^1 start "" /B cmd /c ping localhost -n 11 ^>nul timeout /t 10 /nobreak ) | pause Echo waited timeout /t 12 /nobreak

All the statements inside () are executed first, wait for them to complete, then last two lines are executed. All commands begining with start are executed simultaneously.

These links are helpful: https://stackoverflow.com/a/43762349/9689416 and https://ss64.com/nt/start.html

0

Or you can use this: http://www.out-web.net/?p=167

I did it, it works, however it happens sometimes that the PID of the finished process is immediately re-used by another Windows processus which just started, and it makes the detection of the end of your processus hazardous/erroneous.

3
0

You might look at the Powershell Start-Job cmdlet. It should allow you to do this. More here Powershell is Microsoft's shell of choice. It is much more powerfull than cmd scripts and well worht the investment of your time

5
  • This does not wait for all of them to terminate? Mar 26, 2012 at 17:17
  • It certainly can depending on how you write it. You can poll the started jobs to see whether they've completed and exit your script once they have. The code on that page processes a whole list of jobs, three at a time and waits for them to exit.
    – uSlackr
    Mar 26, 2012 at 17:42
  • Uhm, never did anything before with Powershell. I tried to change the example to run arbitrary processes and wait for them to complete and after about 30 mins and 100 lookups on M$ technet I gave up. I'm sorry if I hurt anybody's feelings but Powershell is crap. It's easier to do this in C (which I think is telling...) Thanks anyway, I bet someone will still find this useful.
    – Sergio
    Mar 27, 2012 at 11:35
  • If there's one thing Powershell isn't is crap and you won't hurt my feelings if you don't use it. You will not, however, learn it in 30 mins of searching.
    – uSlackr
    Mar 27, 2012 at 12:28
  • 1
    PowerShell isn't crap, it's just a full programming language one has to learn, instead of a traditional sh-like syntax. I remember feeling exactly the same way about Perl, Python, Tcl, C#, and every other language I tried to learn "for the lulz" without any real use yet. (When I actually needed them, though, it went fairly easily.) Mar 27, 2012 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .