#!/usr/bin/perl require 5; use strict; # sburke@cpan.org Time-stamp: "2005-08-19 00:35:48 ADT" # desc{ wait until a certain time, or until a process ends } my @pids; my $pause; # usage: after 10m8s # (sleeps for whatever number of seconds that is) # or after 23355 342 342 636 # (wait until all these processes are done) # after 3253 'foobar' # (wait undel process 3254, and all processes # whose ps listing contains 'foobar', are done) # # You can mix substrings and process numbers, but a time # duration (22m, 9s, 2m18s, 10h9m, etc.) can't be mixed with # anything else { my $self = $$; my %active; foreach (`ps ax`) { if(/^\s*(\d+)\s([^\cm\cj]+)/os) { $active{$1} = $2 if $1 != $self; } } #print scalar(keys %active), " actives\n"; my($num,$line, @matching); foreach my $x (@ARGV) { if($x =~ m<^\d+$>) { die "Can't specify a process along with a pause-time!\n" if defined $pause; die "No such active process pid as $x\n" unless exists $active{$x += 0}; push @pids, $x; } elsif (length $x and $x =~ m<^ (?:(\d+)d)? (?:(\d+)h)? (?:(\d+)m)? (?:(\d+)s)? $ >sx and defined $+ # sanity check ) { die "Can't specify a pause-time along with a process!\n" if @pids; $pause = ( ($1 || 0) * 86400 ) + ( ($2 || 0) * 3600 ) + ( ($3 || 0) * 60 ) + ( ($4 || 0) ) ; } else { die "Can't specify a process along with a pause-time!\n" if defined $pause; @matching = (); while(($num,$line) = each %active) { #print "No $x in $line"; next if -1 == index($line, $x); print "$num << $line\n"; push @matching, $num; } die "\"$x\" matches no active processes\n" unless @matching; push @pids, @matching; } } } $| = 1; if(defined $pause) { print "Sleeping for $pause seconds\n"; exec("sleep $pause") || die "Couldn't exec?"; } print "Waiting for [@pids] to finish...\n"; my(@new_pids); while(@pids) { @new_pids = (); #print " checking for [@pids]...\n"; foreach my $line (`ps @pids`) { ##print " ... $line"; #print("OK, continuing $1\n"), push @new_pids, $1 if $line =~ m/^\s*(\d+)\s/; } @pids = @new_pids; sleep 5; } #print "OK...\n"; exit;