#!/usr/bin/perl -w # procwait.pl - a script for job control # # By Jeff Winston http://www.kwcpa.com/tools # Rev. 1.0, 2/19/04 Copyright (C) 2004 # # --------------------------------------------------------------------- # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # #of the License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # #along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA. or check out http://www.gnu.org/copyleft/gpl.html # --------------------------------------------------------------------- # for a thorough description, type Procwait.pl with no parameters use Fcntl; $| = 1; # enable autoflush $match=1; $wait=0; $exact=0; $start=0; $file=0; $offset=0; $success=0; while (($success == 0) && ($#ARGV > -1)) { $arg = shift @ARGV; if ($arg eq "-e") { $exact=1; } elsif ($arg eq "-d") { if ($#ARGV == -1) { $success=1; $proc = "-h"; } else { $offset = shift @ARGV; $offset = $offset * 60; } } elsif ($arg eq "-w") { $wait=1; } elsif ($arg eq "-s") { $wait=1; $start=1; } elsif ($arg eq "-f") { $file=1; } else { $proc = $arg; $success=1; } } if (($#ARGV == -1) || ($success == 0)) { print "procwait.pl by Jeff Winston\n\n"; print "\nUsage: procwait [-e] [-d start_delay (minutes)] [-w | -s | -f] string command\n\n"; print "Procwait typically performs a unix ps -ef command every minute. With no parameters,\n"; print "it will wait until it can no longer find a process containing in the ps report\n"; print "before issuing as a background job. Thus, the new job starts when the\n"; print "one specified by completes. can be multiple tokens, and is run in \n"; print "a c-shell by default. This basic behavior can be modified with parameters as follows:\n\n"; print "-e Forces an exact complete match on .\n"; print "-s Forces reverse behavior. Procwait waits until appears. \n"; print "-w Causes Procwait to wait for to appear, and then disappear.\n"; print "-f Causes Procwait to wait for the existence of a file instead of a process. is the\n"; print " complete filespec and Procwait launches the command when it first finds that the file \n"; print " exists. Thus, a process on another computer could touch a file to trigger Procwait.\n"; print "-d Adds a delay (in minutes) to the start time so that multiple activations of Procwait \n"; print " pended to the same event avoid interacting with each other\n\n"; exit; } $ostype = `echo \$OSTYPE`; chomp $ostype; chomp $proc; while ( (($start==0) && ($match == 1)) || ($wait==1)) { if ($file == 0) { $match=0; if ($ostype eq "linux") { @data = `ps -efw`; } else { @data = `ps -ef -o 'args'`; } foreach $line (@data) { chomp $line; if (!($line =~ "procwait")) { @words = split (" ",$line); if ($#words > 0) { { if ((($exact == 1) && ($line eq $proc)) || (($exact == 0) && ($line =~ $proc))) { $match=1; $wait=0;} } } } } } else { if (-e $proc) { $match=0; } } `sleep 60`; } `sleep $offset`; $dir = `pwd`; chomp $dir; chdir $dir; system(@ARGV); exit;