I have a simple GUI (created using tkinter) that I want to run at a specific time of day on a Raspberry pi 3. Bellow is the code snippet I used in crontab. I invoked the crontab manager using sudo crontab -e.
0 18 * * * cd /home/pi/gui && python3 gui.py
For the moment, I can execute the GUI by invoking it directly via the Pi's command line. However, it doesn't work when I try to do it using cron. I also tried to switch to a basic python script (writing to a file) and that worked. Is there a specific weird interaction that I need to be aware of?
My setup: raspberry pi 3, python 3, raspi-screen, tkinter (latest version as far as I know)
The sudo will run without a tty and display that is why you command won't work.
Try having xvfb installed and use
0 18 * * * cd /home/pi/gui && xvfb-run python3 gui.py
Update-1: 22-Jun-18
If you want to use your actual display then you need to make sure that you use below command
XAUTHORITY=/home/<user>/.Xauthority DISPLAY=:0 python3 gui.py
And also make sure the cron is for your user. The default DISPLAY
is :0
.
When you have a XServer (GUI display), you cannot just connect to it without an authorization. When system start it creates a file and that location is stored in environment variable XAUTHORITY.
When you run a cron, you have limited environment variables. There is no existing XAUTHORITY
or DISPLAY
defined
to be able to connect to display you need. So you need to define every environment variable that would be required by your program
So you define DISPLAY=:0
to select the default display and you need to set XAUTHORITY=/home/<user>/.Xauthority
to prove that you are authorized to connect to the display