How can I test if a program is running from within a script

Your apparent question: testing the existence of a process with a given PID

ps -p $pid is POSIX and should work on any modern non-embedded unix (not antiques or Minix or BusyBox).

A simple and portable way to test whether there is a process by a given PID is kill -0 $pid. This only works if you can send a signal to the process (signal 0 behaves like a signal that is always delivered but has no effect), meaning that kill is running under the process's effective UID or as root. If kill is running as a different user, you can test whether it signals “no such process” (ESRCH) or “operation not permitted” (EPERM), but from the shell this requires knowing how your implementation will format the error message.

Your underlying design: testing whether a process has completed

Your proposed design has a major flaw: how do you know whether that PID is the bunzip process that you were waiting for? Maybe bunzip has finished and there's now another process with its old PID. The only place where you can reliably wait on the termination of a process is in its parent.

A better approach would be to trigger on the existence of the uncompressed file and either the non-existence of the compressed file, or to check that the uncompressed file is not opened by any process (with lsof).


I see nothing wrong with using ps, but here's an alternative

if [ -e /proc/${PID} -a /proc/${PID}/exe ]
    % process active
else
    % process no longer active
fi