csh-script to run a job after an existing job terminates
Stan Tazuma
stan at amc.UUCP
Fri Jul 25 09:30:03 AEST 1986
In article <515 at cubsvax.UUCP> peters at cubsvax.UUCP (Peter S. Shenkin) writes:
>
>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.
>
I think it's a useful tool, but there are simpler ways to do it (at
least under a BSD Unix (which includes the Ultrix you're using)).
Here's a program I came up with a while back. It's called waitp.
It has the same args. as "after".
------------waitp.c------------
/* this program will wait until a given process dies
* e.g. waitp 10309 10
*
* 3/12/85 - skt
*/
#include <stdio.h>
#define DEFAULT_PAUSE 30
extern errno;
main(argc,argv)
int argc;
char **argv;
{
char *cmdname = argv[0];
int pid;
register ret;
register sleep_time = (argc > 2) ? atoi(argv[2]) : DEFAULT_PAUSE ;
if (argc == 1) {
fprintf(stderr, "usage: %s pid [ pause-time ]\n", cmdname);
exit(1);
}
pid = atoi(argv[1]);
while ((ret = getpgrp(pid)) >= 0)
if (sleep_time > 0) sleep(sleep_time);
/* getpgrp returns -1 when the process goes away, and
* sets errno == 3 (ESRCH in /usr/include/errno.h)
*/
/* printf("getpgrp returned %d\n", ret); */
/* printf("errno is %d\n", errno); */
exit(0);
}
-----------------
The way to use this is as follows. Suppose you start a job in the
background. Then you want to run another job after that one completes.
First do a "jobs" to find out the pid of the first job; let's suppose
the pid is 12345. Then run the job:
waitp 12345; <arbitrary command> &
--------------
If you want to do it as a shell script, the ps command can be used
to look at an arbitrary process, by pid. E.g.,
--------------(first, in csh since your script was in csh)
#! /bin/csh -f
set pid = $1
if ($#argv == 2) then
set sleep_time = $2
else
set sleep_time = 30
endif
while (1)
if ( { ps \#$pid } ) then >/dev/null
sleep $sleep_time
else
exit 0
endif
end
--------------(here, in sh since sh probably has less impact on the system)
#! /bin/sh
pid=$1
sleep_time=${2-30}
while :
do
if ps \#$pid >/dev/null
then
sleep $sleep_time
else
exit 0
fi
done
---------------
For AT&T Unix versions, getpgrp() doesn't behave in the above way (waitp.c).
The AT&T ps command can be used to look at a specific process, though
using a different ps argument than above.
Stan Tazuma
Applied Microsystems Corp.
...uw-beaver!tikal!amc!stan
More information about the Comp.sources.unix
mailing list