Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!topaz!husc6!think!mit-eddie!genrad!decvax!tektronix!uw-beaver!tikal!amc!stan From: stan@amc.UUCP (Stan Tazuma) Newsgroups: net.sources Subject: Re: csh-script to run a job after an existing job terminates Message-ID: <212@amc.UUCP> Date: Thu, 24-Jul-86 19:30:03 EDT Article-I.D.: amc.212 Posted: Thu Jul 24 19:30:03 1986 Date-Received: Fri, 25-Jul-86 21:41:49 EDT References: <515@cubsvax.UUCP> Reply-To: stan@amc.UUCP (Stan Tazuma) Organization: Applied Microsystems Corp.; Redmond, Wa. Lines: 95 In article <515@cubsvax.UUCP> peters@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 #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; & -------------- 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