Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!cmcl2!rna!cubsvax!peters From: peters@cubsvax.UUCP (Peter S. Shenkin) Newsgroups: net.sources Subject: csh-script to run a job after an existing job terminates Message-ID: <515@cubsvax.UUCP> Date: Mon, 21-Jul-86 17:59:37 EDT Article-I.D.: cubsvax.515 Posted: Mon Jul 21 17:59:37 1986 Date-Received: Tue, 22-Jul-86 03:33:41 EDT Reply-To: peters@cubsvax.UUCP (Peter S. Shenkin) Organization: Columbia Univ. Bio. CG Fac., NY Lines: 94 DESCRIPTION: after: a procedure that waits until a particular running process terminates, then initiates a new process. This runs under csh, but should be easily translatable to bsh or ksh. USAGE: after pid shellscript [seconds] "pid" is the pid number of a running job. "shellscript" is the name of an executable file which contains commands to be executed once the process number "pid" has terminated. "seconds" is an optional integer which specifies how often "after" will test for termination of process number "pid". If not specified, 900 seconds (i.e., every 15 minutes) is the default. MOTIVATION: I often run very long, cpu-intensive jobs on our VAX 11/780, which runs ULTRIX. If I have two of these jobs running simultaneously, they generally take considerably longer to complete than if they run sequentially, even if I'm the only one on the machine. In addition, if they run sequentially, I get to see the results of the first long before the second terminates. If I know in advance I'm going to run two in a row, I can execute a script that runs them in order, or else enter the two jobs on a line separated by a semi-colon. Sometimes, however, I don't decide to run job2 until job1 has already started; also, setting up such a job sometimes takes a good deal of preparation, and I'd like to start job1, then turn my attention to preparing the input for job2, then tell job2 to start after job1 finishes, then go home. INSTALLATION: Install the first file, "after", in your ~/bin, and the second file, "awkfinafter", in your ~/etc. If you don't like these directories, pick ones you do; if you change the directory for awkfinafter, you'll have to change the assignment of awkfin in "after" to correspond. PHILOSOPHICAL RUMINATIONS: This script seems too simple to post, yet too useful not to. The way it works is to create an awk program file using sed to insert the process id supplied to after. Then the output of ps is awk-ed, and if the process id is not found, the shellscript given to after is executed. If it is found, after "sleep"s for the time interval given (or for a default time of 15 minutes) before trying again. (It might sound simpler to search the output of ps using "fgrep pid", but ps will return a line for the fgrep job, so the search will always succeed!) Note that "after" will not terminate until the script it runs terminates; that means that if you've used "after" to queue up job2 to run after job1 terminates, you can use the pid of that "after" to run a second "after" which queues up job3, and rest assured that job3 will not commence until job2 terminates. And so on ad infinitum (or at least ad nauseum). Enjoy! If you like it a lot or think it's dumb or something drop me a line. Peter S. Shenkin Columbia Univ. Biology Dept., NY, NY 10027 {philabs,rna}!cubsvax!peters cubsvax!peters@columbia.ARPA ----------------------file $home/bin/after begins here-------------------------- # # "after". 21jul86. runs a shell script after a running process has finished # Peter S. Shenkin Columbia Univ. Biology Dept., NY, NY 10027 # {philabs,rna}!cubsvax!peters cubsvax!peters@columbia.ARPA # parse command line, and set sleep interval if not specified if( $#argv == 3 ) then set seconds=$3 else if( $#argv == 2 ) then set seconds=900 else echo 'usage: after pid shellscript [seconds, default=900 ]' exit endif # initialize internal variables set awkfin=$home/etc/awkfinafter set awkfout=/tmp/after$$ # create awk program file sed -e s/@@/$1/ $awkfin> $awkfout # take a breather for $seconds; check for pid; # when no longer found, run script ($2), cleanup and exit while 1 sleep $seconds if( 0 == `ps | awk -f $awkfout` ) then $2 /bin/rm $awkfout exit endif end ----------------------file $home/bin/after ends here-------------------------- ----------------------file $home/etc/awkfinafter starts here------------------ BEGIN { found = 0 } $1 == @@ { found = 1; exit } END { print found } ----------------------file $home/etc/awkfinafter ends here------------------