Is there a way to show notification from bash script in Ubuntu?

105

36

Most of the application can show nicely formatted notification on events that appear on top right corner of the screen. I'm about to write a bash script that will do fairy long processing in the background and I really want to know when it is finished. How can I show that nice notification from a bash script?

vava

Posted 2009-08-29T10:31:30.763

Reputation: 5 238

1How can I trigger a notification when a job/process ends? – phuclv – 2015-07-27T11:59:44.410

Bonus question: Can you put the notification on certain output only? What if you use screen mirroring? – jarno – 2020-02-03T13:36:35.140

Answers

128

If you're using the new notification system in Jaunty, you want the notify-send command

notify-send - a program to send desktop notifications

SYNOPSIS

With notify-send you can sends desktop notifications to the user via
a notification daemon from the command line.  These notifications can be
used to inform the user about an event or display some form of information
without getting in the user's way.

OPTIONS

-u, --urgency=LEVEL
Specifies the urgency level (low, normal, critical).

-t, --expire-time=TIME
    Specifies the timeout in milliseconds at which to expire the notification.
-i, --icon=ICON[,ICON...]
    Specifies an icon filename or stock icon to display.
-c, --category=TYPE[,TYPE...]
    Specifies the notification category.

user4358

Posted 2009-08-29T10:31:30.763

Reputation:

3

The --expire-time parameter does not work on Ubuntu thanks to this "feature" (bug). https://bugs.launchpad.net/ubuntu/+source/notify-osd/+bug/390508

– axiopisty – 2016-04-18T15:43:22.520

I found that notify-send is inhibited during video/audio playback. While this is a valid use case, if you still want to see them then --urgency=critical must be added. – ccpizza – 2017-02-20T07:36:13.757

I'm using jessie (Linux raspberrypi 4.4.50-v7+), on raspberry, install notify 'libnotify-bin', but it don't bring any notification! -> notify-send Test "Hello World" – Dr.jacky – 2017-03-11T09:22:19.173

Thanks buddy. I used this to send me a notification every 10 minutes to blink eyes using cron as a reminder to follow 20-20 (10-10 rule in my case) rule. ;) – Jasser – 2017-10-28T15:40:17.610

To make it pop up a window with an "OK" and "Cancel" button use notify-send --expire-time=0 "Hello World" or notify-send -t 0 "Hello world" – Gabriel Staples – 2018-04-02T17:55:32.347

Note, however, that otherwise the --expire-time or -t option is worthless (ignored), unfortunately, and the default of 10 sec or so applies to all notifications regardless of what you want. Pretty stupid if you ask me! https://askubuntu.com/questions/110969/notify-send-ignores-timeout

– Gabriel Staples – 2018-04-02T18:01:14.330

I couldn't take it; I had to write my own answer :) https://superuser.com/a/1310142/425838

– Gabriel Staples – 2018-04-02T18:15:26.573

4Thanks, just found it myself :) apt-get install libnotify-bin have to be run before to get it. – vava – 2009-08-29T10:39:15.133

How does the root user send a message from crons, init.d, etc? – Lance Caraccioli – 2013-11-22T04:38:41.613

6Eg: notify-send Test "Hello World" – Thaha kp – 2014-06-04T08:26:30.263

31

Found another way, through Zenity

echo 'message:hi' | zenity --notification --listen

(This also has the benefit of already being installed on Ubuntu.)

vava

Posted 2009-08-29T10:31:30.763

Reputation: 5 238

I like Zenity in that it supports user interactions for dialogs (unlike notify-send) – Waffle's Crazy Peanut – 2018-01-21T13:32:28.167

12

For KDE users:

$ kdialog --title "Long process completed!" --passivepopup "This popup will disappear in 5 seconds" 5 &

kolypto

Posted 2009-08-29T10:31:30.763

Reputation: 2 861

Is it possible to set an icon for the notification? – Malabarba – 2012-05-14T04:18:17.263

12

There's also xmessage that will pop-up a window, so it should work on any X11 system.

Pro: It also allows interactively prompting the user with buttons.

Con: Like any pop-up alert, it typically receives focus, so if you're in the middle of typing it can disappear before you read the message.

NVRAM

Posted 2009-08-29T10:31:30.763

Reputation: 778

It's not available in the default Xorg installation of Arch Linux either. – friederbluemle – 2018-01-22T08:06:28.960

You can also get a popup window with an "OK" and "Cancel" button via notify-send --expire-time=0 "Hello World" or notify-send -t 0 "Hello world". Otherwise, however, the -t option is ignored due to some stupid "design decisions": https://askubuntu.com/questions/110969/notify-send-ignores-timeout

– Gabriel Staples – 2018-04-02T18:04:11.720

I couldn't take it; I had to write my own answer :) https://superuser.com/a/1310142/425838

– Gabriel Staples – 2018-04-02T18:15:16.497

4Con: It looks ugly as hell, and also is a super tiny window which is not always obvious to the user. Anyways, it is universal though. :) – Nik Reiman – 2013-05-06T11:37:50.493

xmessage doesn't work in Fedora though. Its not installed by default. – Abhay Mittal – 2014-06-03T16:47:28.580

5

Popup notification that auto-closes after 10 seconds:

notify-send "Hello world"

Source: https://superuser.com/a/31919/425838

Popup window with buttons to click:

[window does not get auto-focus]

notify-send -t 0 "Hello world"

Source: myself; note: -t is ignored for all values except 0--how stupid. :(


OR
[window gets auto-focus]

zenity --info --title "Hello" --text "World"

Source: https://askubuntu.com/a/804475/327339


OR

[MY FAVORITE, since the window auto-closes after the specified --timeout in seconds]

zenity --info --title "Hello" --text "World" --timeout=2

Source: myself reading the man pages: man zenity


OR
[super ugly-looking]

xmessage 'hello world'

Source: http://www.linux-commands-examples.com/xmessage

Gabriel Staples

Posted 2009-08-29T10:31:30.763

Reputation: 625

3

There exists a cross-platform solution called Yfiton:

$ yfiton -n desktop -Pmessage="Lunch time!" -Pposition=TOP_RIGHT

Laurent

Posted 2009-08-29T10:31:30.763

Reputation: 131

2

In a shell script, you can also call the osd_cat utility from libxosd.

geek

Posted 2009-08-29T10:31:30.763

Reputation: 7 120

1This is a little bit different as it doesn't use ubuntu desktop notifications. – vava – 2009-08-29T13:43:40.187

Yes, this is an alternative that you can use with any Linux distribution and any WM/DE. – geek – 2009-08-30T13:39:27.677