Get desktop notifications from Emacs shell commands

When interacting with the operating systems I always use Eshell because it integrates seamlessly with Emacs, supports (remote) TRAMP file names and also works nice on Windows.

After starting shell commands (like long running build jobs) I often lose track the task when switching buffers.

Thanks to Emacs hooks mechanism you can customize Emacs to call a elisp function when an external command finishes.

I use John Wiegleys excellent alert package to send desktop notifications:

(require 'alert)

(defun eshell-command-alert (process status)
  "Send `alert' with severity based on STATUS when PROCESS finished."
  (let* ((cmd (process-command process))
	   (buffer (process-buffer process))
	   (msg (format "%s: %s" (mapconcat 'identity cmd " ")  status)))
    (if (string-prefix-p "finished" status)
	  (alert msg :buffer buffer :severity  'normal)
	(alert msg :buffer buffer :severity 'urgent))))

(add-hook 'eshell-kill-hook #'eshell-command-alert)

alert rules can be setup programmatically. In my case I only want to get notified if the corresponding buffer is not visible:

(alert-add-rule :status   '(buried)     ;only send alert when buffer not visible
		  :mode     'eshell-mode
		  :style 'notifications)

This even works on TRAMP buffers. Below is a screenshot showing a Gnome desktop notification of a failed make command.

../../img/eshell.png
Gnome Desktop notifications