Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
Doing so can harm your data! –  Santosh Kumar Mar 26 '12 at 16:27
7  
@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 '12 at 16:37
    
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. –  grawity Mar 26 '12 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 '12 at 10:42
    
Reinventing the wheel is a good thing if currently existing wheels are square... –  grawity Mar 27 '12 at 13:24

5 Answers 5

up vote 4 down vote accepted

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!
share|improve this answer
    
Yes, that would work but it's a real pain in the ass :) I was looking for a more elegant solution, namely a single script or a pre-packaged executable that handles everything. I'll just piece a few lines of C code together to do that if I cannot find anything better, but let's wait and see first :) Thanks anyway for bringing this up, this is the best solution so far. –  Sergio Mar 27 '12 at 9:42
    
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 '12 at 23:59
    
I'm happy with this solution. –  user1068446 Jul 18 '13 at 5:02

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.

share|improve this answer

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

share|improve this answer
    
This does not wait for all of them to terminate? –  Tom Wijsman Mar 26 '12 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 '12 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 '12 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 '12 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.) –  grawity Mar 27 '12 at 12:30

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.

share|improve this answer
    
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 '12 at 9:42
    
@Sergio: What do you mean by "kept waiting"? –  Harry Johnston Mar 27 '12 at 23:33

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.

share|improve this answer
    
Please summarize what that link says. If the page goes down, this answer will not be helpful. –  Kevin Panko Jan 17 at 20:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.