UNIX shell commands for job notifications

published on 2007-05-20 in computing

Ever spawn off a big job and immediately realize it'd have been great to get a notification when it's done? If it still has hold of your terminal, you could blindly type the next command and hope it works. But if it's forked into the background, that won't even work. Here's how to do it:

Find the process ID of your job. In my case it was 22603:

$ ps auxw | grep batch_process
user   22603  0.0  0.1  8044 2076 pts/18   S+   01:24   0:00 batch_process -bg

Great, now that we have it, use it like this below:

$ ( while ps 22603; do sleep 60 ; done ; date | mail
you@yourcompany.com -s "process done" ) &

Now I can log out and wait to get a mail on it.

The same thing works when waiting for a machine to come back up:

until ping -c 2 hostname.domain.com ; do sleep 10 ; done ; date | mail you@yourcompany.com -s "hostname back up"

Alter the number of seconds of sleep for higher resolution on the checks. The above will send 2 ping checks every 10 seconds. Close enough for rock and roll.

I learned something just now while double-checking my ping syntax: "-a Audible ping." Yes, ping will bonk your shell on each packet return. Whoah cool