Running a tkinter GUI using crontab
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
definedto 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
Raspbian 10 (buster): the only missing part - in my case - was to inform the cron-job using gtk, which display it has to use, by insertingDISPLAY=:0
. I used a bash-script to start the python script using tk; This could be a python script as well.
@reboot DISPLAY=:0 /home/pi/path/to/my/script.sh start
Useful for debugging:
@reboot DISPLAY=:0 /home/pi/path/to/my/script.sh start >> home/pi/Desktop/cron.log
(in one line)
Using cron as user pi (not as root), it was necessary to place the cron.log
file in some /home/pi/...
folder to give writing access for the log-files.
Maybe Raspbian needs some time to setup everything before an applicaiton can be started; I used:
@reboot sleep 10 && DISPLAY=:0 /home/pi/path/to/my/script.sh start
In place of @reboot the usual cron settings can be used, e.g.:
*/5 * * * *
(rest of line see above)
for every 5 minutes to start.