Some people will have come across this: They’ve started some long-running process, e.g. some computation for their thesis, and want to be notified when it’s done. Depending on the setup, they can’t just background it and run wait in the shell.

Or you might want to run some expensive process somewhere, but there is some larger thing going on right now, so you want to wait for that to finish (you know, it’s often better when people don’t fight for memory and put all the load on the swap drive…).

Or you need to monitor some process that might crash, and want to schedule a notification or restart.

Here’s how I’m doing that now:

busywaitpid command-of-process && notify-send "Computation is finished!"

Which will singnal a popup bubble when the process is finished.

Here’s the script:

#!/bin/sh
mypid=$$
pid=`pgrep -of $@ | grep -vE "^$mypid\$"`
if [ -z "$pid" ]; then
  echo "No pid found." >&2
  exit 2
fi
echo "Waiting for pid $pid."
while test -d "/proc/$pid"; do
  sleep .1
done
# Make sure it's gone...
test -d "/proc/$pid" || exit 1
exit 1

Note that it will wait for the oldest (-o) process where the full command line (-f) matches the given parameter.

Bugs: it doesn’t handle when there are multiple processes matching the query - it will use the oldest (as given my pgrep).

[Update: Specto, aptitude install specto, is a GNOME GUI application that you should be able to use for this purpose (amongst others such as website change monitoring)]