I have a Python script with the following code:
import subprocess
import sys
default = "Take a 20 second break - look at least 20 feet away!"
message = sys.argv[1] if len(sys.argv) > 1 else default
def sendmessage(message):
subprocess.Popen(['notify-send', message])
return
sendmessage(message)
called takebreak.py, which will send a system notification.
When I try to automate it using crontab like so:
* * * * * /usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py
It doesn't work. Running the command
/usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py
in the terminal does work, which means it's not a file location problem, but rather something to do with cron. Any ideas?
EDIT1:
After debugging and looking at the logs, I can verify that cron is actually executing the commmand export DISPLAY=:0; /usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py every minute like I set it to do, but for some reason this command, while it should send a system notification, is not doing so.
Any ideas?
EDIT2:
The solution was to add some address bus thing (forget the exact code) that I found in another post, that ended up fixing it. Unfortunately, none of the answers or comments here helped with solving the problem, but thanks regardless!
1 Answer 1
Most likely, the problem is that notify-send is not in your $PATH when running from crontab. First, figure out where it's stored:
$ which notify-send
/usr/bin/notify-send
For me, it's in /usr/bin.
At the top of your crontab file (crontab -e), set $PATH:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
If you want to include whatever $PATH may have already been set before (safer), do this instead:
PATH="${PATH}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Make sure this includes the directory where your command is installed, if it's not installed in /usr/bin.
Of course, the other option, is to simply specify the full command path in your Python script:
subprocess.Popen(['/usr/bin/notify-send', message])
8 Comments
$PATH, as well as changing the Python script to use the absolute path to the command.cat /var/log/syslog | grep crontab returns (among edits and such) the following line: Mar 30 19:31:01 polo cron[634]: (polo) RELOAD (crontabs/polo) >/tmp/cron-error.log 2>&1 to the end of your crontab line, then look at /tmp/cron-error.log and see what it says.
* * * * * export DISPLAY=:0; /usr/bin/notify-send blahdoes not display anythingenv|grep DBUSand hardcode it in the crontab, just like$PATHin Will's answer. That will tell you if it solves the problem. If it does, there are a couple of suggestions on how to do it, in that answer I linked to. It would be easier if it were a bash script, you'll have to figure out the best way to do it, either from Python or by dumping the environment variable somewhere and using it from the crontab.