Get notifications in ubuntu when command line tasks end

Intro

Often, when working in the terminal, you'll find yourself running a command that takes a non-trivial amount of time and you don't want to just stare at the screen until it finishes.

So you switch tabs/windows and do something else in the meantime. Problem is, when is the other task finished? You don't want to waste time checking too often nor too late...

So what you want is a notification. One that lets you carry on merrily until the original command is actually finished.

It turns out that many .bashrc files come with an alias called alert and, some SO answers even improve upon it.

Desktop notifications with notify-send

Here's the one I'm using lately and has served me well:

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)"  
"$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

As the comment says, using it is just a matter of writing the command you want, a semi-colon and the alias alert (Remember that semi-colon ; means execute after the previous command is finished, no matter the return code, unlike && which only executes the next command if return code is 0 (success).

So if you're compiling and running tests in a project you could just do:

make test; alert

and you'll get notified whenever make test ends.

But what if you decided running that lengthy task is a good moment to step away from your computer and take a coffee break or talk with a coworker? How will you know when it's done if you're not in front of the computer to see the desktop notification?

Email notifications

That's when email comes in handy. You just gotta take your phone with you and have access to an SMTP server.

$ sudo apt install mailutils

The logic is the same as before, once the command is done, execute the "alert".

If you want to do it in python, here's a simple way to go about it.

Just make sure it doesn't go to SPAM.

Cheers


Was this helpful? Do you do it another way? All comments are welcome!

Comments !

links

social