after – wait until an unrelated process has terminated
after [ -x | -z ] pid
after lets you specify a process by its numeric PID, and then waits until that process has terminated. If possible, it returns the process's exit code as well.
You might use
after if you had started a long-running process in one window and then realised that you should have told the shell to do something else after it finished. For example, after typing ‘
make’ to begin a long build run, you suddenly realise you should have typed ‘
make && ./runtests.sh’, so that you wouldn't have to be physically present to kick off the test run after the build completed, and so that you could take one long coffee break instead of two short ones. In this situation you could use
after to solve your problem: use
ps(1) to look up the process ID of the
make process, and then type a command such as ‘
after 34530 && ./runtests.sh’ in a second terminal window.
The operation of waiting for an arbitrary process is not an easy one on Unix, so the
after command supports multiple methods of achieving it and will try them in order until one works. Some methods allow the exit code of the process to be retrieved, in which case
after will return the same value; others do not.
after to only attempting methods which retrieve the process's exit code. These methods typically require
after and the target process to be running under the same user ID. If no available method works,
after will fail.
after to always return 0 (success) after detecting that the process has terminated, whether or not its exit code was available.
If it settles on a method which provides the target process's exit code,
after will return the same code itself (unless you disable this with the
-z option). Hence, you can use the shell
&& operator to run other commands which are conditional on the target process succeeding (as in the example above).
If it has to fall back to a method which does not provide the exit code,
after will return 0 (success) when the process terminates.
If something goes so badly wrong that
after was unable to reliably wait for the process at all, it will return 127.
There are not enough methods supported.
The whole concept of
after has an inherent bug in the form of a race condition: if you're not careful, the target process could terminate in between you looking up its PID and typing the
after command. If your process looks as if it might terminate within the time it takes to type the command, it's probably better not to try using
after at all.
after is free software, distributed under the MIT licence. Type ‘
after --licence’ to see the full licence text.