Notify-send doesn't work from ROOT crontab
This script works fine in user's crontab, but I need run from ROOT crontab, but not work:
#!/bin/sh
# cron.sh
# Notifies the user of date and time
source /home/user/.bashrc
pid=$(pgrep -u user openbox | head -n 1)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
export DBUS_SESSION_BUS_ADDRESS=$dbus
export HOME=/home/user
export DISPLAY=:0
/usr/bin/notify-send 'title' "$(/bin/date)"
The script was found here: crontab script
The script is placed on /home/user/cron.sh with all permissions(777). My root crontab is:
* * * * * /home/user/cron.sh
If I use this script in normal user crontab it works and show the popup text, but not from root crontab.
Syslog (/var/log/syslog
) not show errors when crontab trigger.
I'm pretty sure notify-send
needs to be run as the user who should receive the notification. Try to replace the last line with this:
su user -c '/usr/bin/notify-send "title" "$(/bin/date)"'
The su user -c
command switches user to user
and -c
passes the following command to the shell.
This statement should switch to the correct user and run the command.
If you want to run the entire script as another user, take a look at this Q&A from Stack Overflow.