[Chicago-talk] Script design question

Leland Johnson easyasy2k at gmail.com
Tue Dec 13 19:17:21 PST 2005

waitpid is a very thin wrapper over the C function of the same name  
that can do quite a lot. Check out `man 2 waitpid` on your system.

Here's some example code with waitpid(-1, 0) semantics. waitpid(-1,  
0) will hang until any child has terminated (returns that child's  
pid) or there are no more children to wait for (returns -1).

I use fork() and exec() here instead of system() or qx// so I can  
tell the processes apart with the pid that fork() returns.

I think this would be the simplest way to solve your problem, but it  
has the disadvantage of looking a lot like C code.

use strict;
use warnings;
use POSIX ":sys_wait_h"; # Not sure if you need this.

my ($pid1, $pid2);

if( ($pid1 = fork()) == 0 ) {
     sleep 10;
     print "child 1\n";
     exit 69;

if( ($pid2 = fork()) == 0 ) {
     print "child 2 execs `date`\n";
     exec("date") or die "error executing `date`!\n";
     # exec never returns on success

my $kid;
# Blocking waitpid.
while( ($kid = waitpid(-1, 0)) != -1 ) {
     my $status = $? / 256;
     if ( $kid == $pid1 ) {
         print "Child 1 exited with status $status\n";
     elsif ( $kid == $pid2 ) {
         print "Child 2 exited with status $status\n";
     else {
         die "Got unexpected child pid. Shouldn't happen.\n";
print "No more children.\n";


On Dec 13, 2005, at 6:24 PM, Young, Darren wrote:

> Looking for some input on a script before I sit down and try to create
> it. The basic need is to launch 4 system commands (at the same  
> time) and
> watch each one for completion and when one is done to start another.
> Basically, it needs to keep 4 child processes running up to a defined
> end.
> If that doesn't make sense, here's what it's for. In order to run
> backups on our mail system we have to use a Sun supplied binary
> (imsbackup). As an argument this binary takes what are called "groups"
> to perform a given backup. Those "groups" are defined in another file
> (backup-groups.conf) and contains lines such as:
>    groupA=a*
>    groupB=b*
> And so on and so forth until the letter Z. groupA are users that start
> with the letter a, groupB are the b's, and so on. Now, since these
> backups take so long to run we're going to run 4 of them in parallel,
> each one against a different group. So, from the command line I would:
>    imsbackup -i -f- /gsbims/groupA > /export/backups/groupA.bkp &
>    imsbackup -i -f- /gsbims/groupB > /export/backups/groupB.bkp &
> Do that for groups A-D and let them run. Then, the first one that
> completed would fall off the "to-do" list and the next letter group  
> will
> be started (E in this case). Then on to F, G, etc. Testing so far says
> that an individual group can take up to 2-3 hours to complete so I  
> have
> to be able to deal with processes that sit and run for a while and
> produce no output. Not sure what exit codes the imsbackup program  
> gives
> back, but I'm betting it's just a 0 or 1.
> Now, if one of them fails all I need to do is log the fact that it did
> and move on. Additionally, I'd like to keep track of the time it  
> took to
> perform each group "thread" and log that as well. I'd prefer not to  
> have
> a script call other scripts as opposed to calling the commands
> themselves, I've found that I lose tidbits here and there from
> sub-scripts. While that's a preference, it's certainly not required.
> The part that I need help with is how to actually spawn each child and
> watch for their outcome (reliably). The waitpid() call seems to  
> wait for
> one particular process to complete or am I reading the docs  
> incorrectly?
> Am I talking about using threads in this case or am I over-engineering
> what I want done? I've done plenty of whil(<PIPE>) stuff previously  
> but
> only on a single process.
> Any thoughts on this would be very appreciated.
> -------------------------------------------------------------
> | Darren Young              | http://www.chicagogsb.edu     |
> | Senior UNIX Administrator | darren.young at chicagogsb.edu   |
> | University of Chicago GSB | darren.young at gsb.uchicago.edu |
> -------------------------------------------------------------
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk

Leland Johnson

More information about the Chicago-talk mailing list