Wouldn’t it be great if you could get pop-up notifications when those long running tasks from the command line finish? Well, you can. Actually, creating desktop notifications from the command line is very simple with the
You can use
notify-send in your scripts or set it to run following a long command that you just typed into the command line. The messages that you create are entirely customizable, and they automatically integrate into whichever desktop environment you’re running, so they don’t look out of place or ugly.
Open up a terminal on your Linux system and create your first notification. Use the one below and see how it works.
notify-send 'Hello World!' 'This is a custom notification!'
Did you see it pop up? Excellent. Try another one.
notify-send 'I sent a notification!' 'Yeah! Another custom notification!'
Now there’s more that you can do to customize your notifications!
Some notifications are more important than others. There are three levels of notifications: low, normal, and critical. Your desktop environment might visually distinguish them, or it might impact how long they appear on screen. You can specify the urgency of your notifications with the
notify-send 'SUPER IMPORTANT!' 'This is an urgent message!' -u critical
See how that differs from a low urgency one:
notify-send 'Eveything is alright' 'Just checking.' -u low
Your desktop environment will determine how they behave.
It’d be even better if you could add a graphical component to these messages, wouldn’t it? Well, you can.
notify-send lets you specify an icon. It already “knows” all of your system icons, so if you look under “/usr/share/icons” you can see what’s available there. Use the name of the icon file without the extension along with the
-i flag to add it to your message.
notify-send 'I broke all the things!' 'Call the admin, NOW!' -u critical -i face-worried
If you don’t like the system icons, you can always point your notifications at custom icons. Just use the path to your icon file.
notify-send 'Super Awesome Custom Icon' 'It is pretty cool, right?' -u normal -i '/home/user/Pictures/icons/excellent-icon.png'
There are some other flags you can explore. To see them all, run
notify-send --help. A couple of useful ones let you specify the application sending the notification and the time that it stays up. So, if you want your notification to stay up for a specific duration, add the
notify-send 'It Broke' 'You command failed' -u normal -t 10000 -i error
-a flag lets you specify the app sending the notification. Not every desktop environment includes this information, but it can help you distinguish when ones can.
notify-send 'My script has a name!' 'The script did something.' -a 'Script Name' -u normal -i face-smile
Scripting is one of the main things that you’d use this whole thing with. It doesn’t have to be a massive complex Bash script either. Say you want a notification when your system is done updating. It can do that pretty easily.
sudo apt update && sudo apt -y upgrade && notify-send 'Updates Complete' 'Your system updated successfully!' -u normal -t 7500 -i checkbox-checked-symbolic
You can get as complex as you want with it, and even create your own wrapper scripts with notifications.
#! /bin/bash if sudo apt install $1; then notify-send "Install Successful!" "$1 installed successfully" -u normal -t 5000 -i checkbox-checked-symbolic; else notify-send "Install Failed" "$1 failed to install" -u critical -i error; fi
That example is still simple, but you can get an idea of what’s possible.
If you’re computer has SSH configured for X server sessions, you can actually send notifications over SSH, too.
ssh -X email@example.com 'DISPLAY=:0 notify-send "HAHA I'm In Your Computer!" "Deleting all your stuff!" -u critical -i face-worried'
Trolling the people on your network is obviously not the only application for this. You do need the login information just like if you were to add SSH in regularly. It won’t maintain the session, though. It’ll just display the message and exit.
Between SSH and scripting, you can do wide array of things with
notify-send. Experiment and see how you can tailor it to your needs.