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.
touch /home/user/script-has-run
. If the file/home/user/script-has-run
exists, then the script has executed. – Artur Meinild Jun 11 '21 at 10:46source
is a bashism - in POSIX /bin/sh you should use. /home/user/.bashrc
but really you shouldn't be sourcing a.bashrc
into ash
shell - if you want features from the user's.bashrc
, switch the shebang to#!/bin/bash
– steeldriver Jun 11 '21 at 11:03