I am trying to run a simple script that checks the battery percentage and shows it through a notification (notify-send
).
The script runs correctly if ran from terminal manually: ~/path-to-script/my_script.sh
or sh ~/path-to-script/my_script.sh
or even bash ~/path-to-my-script/my_script.sh
.
Where correctly means that the notification is shown.
However when trying to run it through cron
no notification appears.
This is what crontab -e
looks like:
*/1 * * * * /usr/bin/sh ~/path-to-my-script/my_script.sh
I have, also, set some environment variables in crontab
with sudo vim /etc/crontab
:
SHELL=/usr/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
And this is the log from sudo service cron status
:
gen 28 17:35:01 my_user CRON[7863]: pam_unix(cron:session): session opened for user my_user(uid=1001) by (uid=0)
gen 28 17:35:01 my_user CRON[7864]: (my_user) CMD (/usr/bin/sh ~/path-to-my-script/my_script.sh)
gen 28 17:35:01 my_user CRON[7863]: (CRON) info (No MTA installed, discarding output)
gen 28 17:35:01 my_user CRON[7863]: pam_unix(cron:session): session closed for user my_user
I am running on Ubuntu 22.04 using i3 as window manager. What could be the cause of this issue?
Please ask if more info is needed.
date >~/hello.txt
and see if the file gets updated. If so, your problem isn'tcron
, but something it is looking for...perhaps that MTA it's complaining about. – Ray Jan 28 '24 at 16:46No MTA installed
message is becausecron
wants to report something and can't - far simpler than installing an MTA would be to redirect the output + error streams to a log file ex.*/1 * * * * /usr/bin/sh ~/path-to-my-script/my_script.sh > ~/cron.log 2>&1
and examine that – steeldriver Jan 28 '24 at 17:17cron
is not able to connect to your user's display - see for example How to use notify-send with crontab? – steeldriver Jan 28 '24 at 17:46Cannot autolaunch D-Bus without X11 $DISPLAY
. So I've setDISPLAY=:0
in my_script.sh according to the value returned byecho $DISPLAY
, I've also setDBUS_SESSION_BUS_ADDRESS
to the value printed byecho $DBUS_SESSION_BUS_ADDRESS
. Still the issue persists. – overmach Jan 28 '24 at 17:48Cannot autolaunch D-Bus without X11 $DISPLAY
I've found this discussion that suggests to prependenv DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
to the command incrontab -e
. It works! – overmach Jan 28 '24 at 18:09