53

I need to show a notification from a cron job. My crontab is something like:

$ crontab -l
# m h  dom mon dow   command
  * *   *   *   *    Display=:0.0 /usr/bin/notify-send Hey "How are you"

I checked /var/log/syslog and the command is actually executed every minute but it doesn't pop up the notification. Can anybody help me understand why?

14 Answers 14

116

I use i3 on Ubuntu 18.04. My way to solve this is:

* * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) notify-send Hey "this is dog!"

Edit 2020: I still use it on Ubuntu 20.04.

10
  • 10
    Can confirm this works on Ubuntu 18.04 where the accepted answer did not.
    – Kyle
    May 5, 2020 at 17:37
  • 8
    Can confirm this works on Ubuntu 20.04 when the confirmed answer did not.
    – user81371
    May 16, 2020 at 5:21
  • 2
    This should be the confirmed answer now. I subconsciously went straight to the accepted answer which didn't work, and therefore initially missed the working solution.
    – loop
    Dec 17, 2020 at 18:52
  • 3
    Can confirm this works on Ubuntu 22.04 when the confirmed answer did not. Dec 13, 2022 at 12:50
  • 3
    this is working for me on ubuntu 22.04 Aug 16 at 6:45
24

I found the answer:

$ crontab -l
# m h  dom mon dow   command
  * *   *   *   *    export DISPLAY=:0.0 && export XAUTHORITY=/home/ravi/.Xauthority && sudo -u ravi /usr/bin/notify-send Hey "How are you"
6
  • 8
    Just adding "export DISPLAY=:0.0" did the trick for me. That said, the cronjob is running under my user account. XAUTHORITY is probably need when run from a different account than the one where the popup needs to appear. Thanks.
    – Ruben
    Jul 15, 2013 at 23:56
  • Thanks, that helped me too (I didn't need to use "sudo") Feb 15, 2014 at 17:05
  • 3
    Me too. I could reduce this command to * * * * * export DISPLAY=:0.0 && notify-send Hey "How are you"
    – TomTom
    Apr 9, 2015 at 9:47
  • 4
    It can be further reduced to * * * * * DISPLAY=:0.0 notify-send Hey "How are you"
    – tripleee
    Mar 27, 2017 at 21:20
  • @DrunkenMaster I just tried this on 18.04; worked for me. The crontab is my own (not root's) * * * * * export DISPLAY=:0.0 && notify-send -t 10000 Hey Hey Oct 18, 2019 at 18:55
7

In Ubuntu 14.04 exporting the display did not work for me. Below is a cron script I'm using to shutdown a virtual machine when a laptop's battery state becomes too low. The line setting DBUS_SESSION_BUS_ADDRESS is the modification that finally got the warnings working correctly.

#!/bin/bash

# if virtual machine is running, monitor power consumption
if pgrep -x vmware-vmx; then

  bat_path="/sys/class/power_supply/BAT0/"

  if [ -e "$bat_path" ]; then

    bat_status=$(cat $bat_path/status)

    if [ "$bat_status" == "Discharging" ]; then

      bat_current=$(cat $bat_path/capacity)

      # halt vm if critical; notify if low
      if [ "$bat_current" -lt 10 ]; then

        /path/to/vm/shutdown/script
        echo "$( date +%Y.%m.%d_%T )" >> "/home/user/Desktop/VM Halt Low Battery"

        elif [ "$bat_current" -lt 15 ]; then
        eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
        notify-send -i "/usr/share/icons/ubuntu-mono-light/status/24/battery-caution.svg"  "Virtual machine will halt when battery falls below 10% charge."

      fi

    fi

  fi

fi

exit 0

The relevant line is here:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";

I found the solution here: https://askubuntu.com/a/346580/255814

1
  • I use archlinux, which uses a different process for the gnome-session, but still with the string gnome-session as a long flag. Thus needed to add -f to the pgrep invocation.
    – lash
    Sep 8, 2020 at 16:26
6

Only this works for me (Xubuntu)

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ)"; notify-send  "hello world" 

If you are in gnome enviroment, you need change xfce4-session to gnome-session

refer: https://askubuntu.com/questions/298608/notify-send-doesnt-work-from-crontab

1
  • 1
    As of ubuntu 17.10 I believe you need to be looking for gdm-x-session. Here's the line I used: eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gdm-x-session)/environ)" May 15, 2018 at 10:43
6

add DBUS_SESSION_BUS_ADDRESS

* * * * * DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /usr/bin/notify-send 'helloworld..' 'msg...'

3

Work for me on fedora 22:

Put this line in the .sh script before notify-send get called:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"
2

On recent Ubuntu versions, the following should work.

#notify_me.sh, can be placed e.g. in your home directory

#!/bin/bash
eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
# the actual notification
DISPLAY=:0 notify-send "Notify me!"

Then you add a line to your user's cronjobs via crontab as usual.

1

Simple and reduced answer:

01 * * * * export DISPLAY=:0.0 && notify-send Hey "How are you"

If you need Xauthority permission, here's a generalizable form using the $LOGNAME variable

01 * * * * export DISPLAY=:0.0 && && export XAUTHORITY=/home/$LOGNAME/.Xauthority notify-send Hey "How are you"

As pointed out by @tripleee, there's no real need for sudo here

0

I created a /usr/bin script that uses the DISPLAY-:0.0 technique http://pastebin.com/h11p2HtN

It doesn't take XAUTHORITY into account. I'll have to investigate that further.

0

Similar to nmax above, I also solved the issue by setting DBUS_SESSION_BUS_ADDRESS environment variable. However, I am using a combination of Linux Mint 19 (xfce4) and XMonad and for some reason, I don't have the process xfce4-session running. Instead, I found that xfce4-terminal is (usually) running, which resulted in the following line in the beginning of my script:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-terminal)/environ)"

This resolved the issue for me.

0

If you are having issue that your notify-send script is working fine locally but it is not working with cron jobs then use this command. and replace "saurav" with your username.

sudo -u saurav DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Your message goes here"
0

In my case, the issue was with using root user with notify-send. I realized this when I saw that sudo notify-send didn't work on the terminal but just notify-send (i.e. with current user) did. So instead of editing the usual /etc/crontab which is used by root, I had a custom cron script created for myusername. Please check my whole answer here.

-1

May be you can try:

* * * * * env DISPLAY=:0.0 sudo -u ravi /usr/bin/notify-send Hey "How are you"

1
  • Downvote: The cron job will already be running as ravi so the sudo is not adding anything useful here.
    – tripleee
    Mar 27, 2017 at 21:21
-3

Try this when you call notify-send in your script:

echo "PASSWORD" | sudo -u USER notify-send "your alert message"

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.