问题
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?
回答1:
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"
Thanks, Ravi
回答2:
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
回答3:
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
回答4:
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!"
回答5:
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)"
回答6:
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.
回答7:
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
回答8:
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.
回答9:
May be you can try:
* * * * * env DISPLAY=:0.0 sudo -u ravi /usr/bin/notify-send Hey "How are you"
回答10:
Try this when you call notify-send
in your script:
echo "PASSWORD" | sudo -u USER notify-send "your alert message"
来源:https://stackoverflow.com/questions/16519673/cron-with-notify-send