2

I've been searching on the internet but haven't found the correct answer to my question. Here is my question in detail:

There is python opening a web browser.I can run it manually on terminal and it worked well.

But when I put it into crontab,the web browser can not be opened. This is my contab command:

*/1 * * * * /usr/bin/python /home/pi/test.py 2>&1 >/dev/null | tee -a /home/pi/test.log

Please notice that this is a DISPLAY problem. Python was executed successfully.

I checked the log created by crontab and it says:

Cannot connect to X server

So I tried to add env variables in my contab and changed it into:

*/1 * * * * export DISPLAY=:0.0 && /usr/bin/python /home/pi/test.py 2>&1 >/dev/null | tee -a /home/pi/test.log

Still it does not work. And I add something else into crontab like:

/usr/bin/xhost +
/usr/bin/ssh pi@localhost -X

But it just does not work!

Now I don't know what to do. Can anyone tell me what is wrong with it and how to solve it? Thank you very much!

Sincerely, Helen

Helen Cui
  • 181
  • 2
  • 11

1 Answers1

5

So excited because I solved this problem! Here's how I realised it:

I write a shell script to run python script. Here's my shell script(named"shell.sh"):

#!/bin/bash 
xhost +local:root 
export DISPLAY=:0.0
python /home/pi/test.py

Here I think xhost +local:root can also be replaced by xhost + localhost. if your user is root.

Here's my crontab command:

 */1 * * * * /bin/bash /home/pi/shell.sh 2>&1 >/dev/null | tee -a /home/pi/shell.log

Now it works perfectly!!

Anyone has the same question with me can have a look at this link: Cannot connect to X server :0.0 with a Qt application

Helen Cui
  • 181
  • 2
  • 11