I am having difficulty running the below simple script from crontab:
#!/bin/bash
notify-send "battery.sh working"
The permissions of the file are rwxr-xr-x
and its running fine with either of the commands bash battery.sh
and sh battery.sh
.
In my crontab, I have tried running it with both bash
and sh
, with absolute as well as local path. My current crontab looks as follows:
* * * * * /home/marpangal/battery.sh
* * * * * sh battery.sh
* * * * * bash battery.sh
* * * * * sh /home/marpangal/battery.sh
* * * * * bash /home/marpangal/battery.sh
However the cron does not execute the script and I get no message from notify-send.
notify-send
needs the DBUS_SESSION_BUS_ADDRESS
environment variable in order to communicate with the current desktop session. Since cron
runs with an almost empty environment, this variable is not available.
But you can set it directly in your battery.sh
script:
export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)
notify-send "Message from cron"
This looks up the process id of your gnome-session
and extracts the DBUS_SESSION_BUS_ADDRESS
(along with its value) from then gnome-sessions
' environment.
Now notify-send
is able to display notifications in your desktop session.