140

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?

Jonik
  • 5,940
vava
  • 5,948

7 Answers7

167

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.
42

Found another way, through Zenity

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

or like this:

zenity --notification --text "System update necessary!" 

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

kolypto
  • 3,121
vava
  • 5,948
19

Tested on Ubuntu 14.04, 16.04, 18.04, 20.04. Screenshots from Ubuntu 20.04.

  1. [WORKS WELL] Popup notification that auto-closes after 4~10 seconds (somehow tied to your OS settings?):

    notify-send "Hello world"
    

    enter image description here
    Source: https://superuser.com/a/31919/425838

  2. Popup window with buttons to click:

    1. Window does NOT get auto-focus: Source: myself; note: for older Unity-based versions of Ubuntu, such as 16.04, -t is ignored for all values except 0--how stupid. :(. For newer Gnome-based versions of Ubuntu, such as 18.04 or 20.04, -t is ignored entirely. Therefore, on older Unity-based versions of Ubuntu, such as 16.04, using -t 0 causes buttons to show up, but on newer Gnome-based versions, it does NOT. That means that for the Ubuntu 20.04 screenshot shown below, the behavior and look of notify-send -t 0 "Hello world" is exactly identical to notify-send "Hello world" above.

      notify-send -t 0 "Hello world"
      

      enter image description here

      On Ubuntu 18.04 or 20.04 or later, just add -u critical to the command instead to get it to stay open indefinitely until you click anywhere on it!:

      notify-send -u critical "Hello world"
      

      enter image description here
      Source: @lucidbrot's comment below this answer, plus my own testing.

    2. OR Window DOES get auto-focus:

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

      Note: the window will NOT close until you click the OK button.
      enter image description here
      Source: https://askubuntu.com/a/804475/327339

  3. [MY FAVORITE] The window auto-closes after the specified --timeout in seconds, OR after you click the "OK" button!

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

    Note: the window WILL automatically close after the specified timeout above, in seconds!
    enter image description here
    Source: myself reading the man pages: man zenity

  4. [super ugly-looking]

    xmessage 'hello world'
    

    Note: the window will NOT close until you click the okay button.
    enter image description here
    Source: http://www.linux-commands-examples.com/xmessage

Play sounds too

  1. If you want to play sounds too, along with the window popup, to signify the completion of a command or something, see my other answers here:
    1. AskUbuntu.com: How to make a sound once a process is complete?
    2. My "Bash one-line alarm script" here: Ask Ubuntu: Alarm clock for Ubuntu. In it, I use the zenity popup window.

See also

  1. My "Bash one-line alarm script" from the command-line, and my custom alarm_timer <seconds> <alarm message> function there.
13

For KDE users:

$ kdialog --title "Long process completed!" --passivepopup "This popup will disappear in 5 seconds" 5 &
kolypto
  • 3,121
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
  • 848
4

There exists a cross-platform solution called Yfiton:

$ yfiton -n desktop -Pmessage="Lunch time!" -Pposition=TOP_RIGHT
Laurent
  • 151
3

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

geek
  • 10,674