Notifications for long running terminal commands

Notifications for long running terminal commands

This is another Easter quicky.

Sometimes you run commands in your terminal that take some time, for example
sudo apt-get update
can be a pain when your web connection is slow. Usually, you would have to check every once in a while but you could also use the notification system.

Using alert

Difficulty level: easy peasy

Ubuntu has one helpful command built-in that is alert which is an alias for notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')" which calls the built-in notification system via notify-send, it’s just a bit more comfortable than that gibberish. You have to append alert to the command you want to run
sudo apt-get update;alert
you can test it by using
sleep 5;alert
Screenshot from 2015-04-05 22:43:11
This is already pretty cool but has one drawback: you have to add it manually.

Using undistract-me

Difficulty level: slightly advanced

I have borrowed this partially from Abishek of It’s F.O.S.S., so half the Kudos go to him.

undistract-me works with bash only and I have personally tested it just with Unity but it should work with other desktops as well, I saw a few mentioned in the various comment sections. It is not part of your default installation but can be installed from official repository using
sudo apt-get install undistract-me

Unfortunately, this doesn’t work automatically, so you have to modify the .bashrc file that is in your home directory. I chose the change that was posted in a comment at WebUpd8 and that is to append this

if ! [ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$last_command_started_cache" ]; then
  # export LONG_RUNNING_COMMAND_TIMEOUT=30
  export LONG_RUNNING_IGNORE_LIST="man info nano mc pkexec gksudo"
  . /usr/share/undistract-me/long-running.bash
  notify_when_long_running_commands_finish_install
fi

at the end of your .bashrc. However, I have added two more lines to the code. The default timeout is 10 seconds, this might be too long or short for you. If yes, remove the hash in front of the first export line and adjust the value to your liking. The second export line contains the ignore list, I have added stuff like nano, mc, info and man, it just makes no sense to get notified when you end them, same for pkexec and gksudo that are just used to start other applications.

Once you have done that restart the terminal you are using and type something like
sleep 11
to test (if you changed the timeout use a value for sleep that is 1 second more).

Drawback of this: you cannot switch it on and off easily. You have the ignore list, but that only work when the command used is not on your histories ignore list.

To uninstall, remove the lines in .bashrc and uninstall undistract-me using
sudo apt-get remove undistract-me

An even more advanced extra:
undistract-me only works, when the window that runs the command is not active. As soon as you use tabs in the terminal that doesn’t make sense and I saw a few requests to change that behavior, so I had a look and tried my best. The following is tested but only to a very limited extent. Use at your own risk.

You can change that behavior by modifying the script, you have to comment out three lines of code in the script: 63, 64 and 87. You do that by adding a hash (#) in front of the respective code in the script. To do that you have to start your editor as root.
Screenshot from 2015-04-05 23:27:31
Then you can get notifications like this
Screenshot from 2015-04-05 23:45:51

Again: I have tested these changes for a while without any problems, however, these changes come with absolutely no warranties, use at your own risk. In other words, if your exploding computer kills your hamster, don’t blame me.

Tl;dr

To get system notifications for long running terminal/shell commands such as sudo apt-get update you can either use the alert alias or the undistract-me script. Both methods have their advantages, choose what you like best.

And if you have tried my little hack, let me know if it worked for you as well in the comment section. Or maybe you have something else to get notifications.

Leave a comment