Notify me when emerge is done

As a Gentoo user, I frequently install new software or update existing packages using emerge. Unlike binary package managers, building packages from source using emerge takes time, and I prefer running it inside a detached screen session, because (a) if I close the session it continues in the background and (b) it actually runs faster when it doesn't need to show all the compilation output in an X terminal.

This has an annoying side effect: I sometimes start a long update process and forget I did it. If things fail (or even if they succeed) I don't know about it.

Today, I solved this problem using notify-send, the libnotify binary client. I've created a short shell script wrapper to emerge which sends me a notification once emerge finishes, along with the original command line arguments and exit status code (0 = ok, failure otherwise):

CODE:
  1. #!/bin/sh
  2.  
  3. ARGS=$@
  4.  
  5. /usr/bin/emerge "$ARGS"
  6. STATUS=$?
  7. if [ "$STATUS" = "0" ]
  8. then
  9.         LEVEL="normal"
  10. else
  11.         LEVEL="critical"
  12. fi
  13.  
  14. /usr/bin/notify-send --expire-time=0 --urgency=$LEVEL "emerge finished" "Exit Code: $STATUS
  15. Emerge args: $ARGS"

If you save this code in a file named emerge-notify (and of course remember to chmod +x this file) you could then do this (from a GNOME terminal or any other X terminal):

CODE:
  1. $ ./emerge-notify -puD world

And you'll get a nice notification when it's done:

"emerge finished" notification

Of course, you need libnotify the notify-send binary and dbus for this.