Getting notified from the command line
When executing long-running commands in the terminal, it is useful to get notified when they are done, instead of checking the terminal manually.
Personally, I am using the Linux notify-send utility program to produce toast-style system notifications. In my desktop environment, notify-send produces notifications at the right level of intrusiveness, and the notification stays in the notification center in case I want to get away from the computer entirely. But nothing stops you from being creative here; macOS users can leverage the native say command to get an audio notification, for instance.
The basic pattern involves putting the notify command of your choice after your long-running task (notice the semicolon):
> ./takes-long.sh ; notify-send "Task finished"
If you want, you can get notified only on success, on error, or have a different message for success and error:
> ./takes-long.sh && notify-send "ended with success"
> ./takes-long.sh || notify-send "ended with error"
> ./takes-long.sh && notify-send "ended with success" || notify-send "ended in error"
> if ./takes-long.sh; then notify-send "ended with success"; else notify-send "ended in error"; fi
While not bulletproof, combining short-circuiting operators && and || together will work as expected as long as the notify-send
command works properly. This style is less verbose than the proper if-else structure shown in the last example.
I also like to alias notify-send to just notify. You might want to do something similar. You can find more about aliases and short-circuiting operators in my Command Line Handbook.
—Petr