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
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"
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.
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.
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
Try this when you call notify-send
in your script:
echo "PASSWORD" | sudo -u USER notify-send "your alert message"
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)"