I'm doing something similar on my home server where I want a quick-and-dirty queuing system for quantum chemical computations. The following is a chunk from the middle of my queuing script:
# If second argument passed, treat as PID to check for
# completion before proceeding
if [ -n "$2" ]
then
while [ `ps aux | grep orca | sed -r 's/[a-z0-9]+[ ]+([0-9]+).*/\1/' | grep "^$2\$" | wc -l` -gt 0 ]
do
sleep 1m
done
fi
The commands inside the back-ticks perform the following:
- Call
ps aux
to get the process list
grep
the name of the executable ("orca
" here) to protect against the rare case where the process of interest has run long enough that the PID usage has wrapped back around and the PID of interest has been assigned to an unrelated process
sed
each remaining process listing, using extended Regex, to leave only the PID number on each line
grep
to check if any of the PIDs found are a match for the target PID
wc -l
provides an easy way to count the number of matching PIDs (the count should only ever be one or zero, but the -gt 0
implementation should be robust against unusual circumstances)
The script then sleeps for one minute (easily customizeable) and repeats the PID check. At whatever point the PID of interest is no longer found, the script moves on. In my case, the downstream commands execute the appropriate computation; you can adapt to perform whatever action(s) you wish.