This question already has an answer here:

a little background as it might be the cause to the problem. I'm running Armbian legacy Jessie on a Orange Pi Zero. It does not include an desktop so I've installed X, lightdm and xfce. I havn't managed to start X on boot so I have a @reboot line in crontab that executes a script that includes this:

while ! ping -c 1 -W 1; do
    sleep 1

everything works perfectly (I have autologin enabled and it starts the Mumble client on 1:0). I then have a python script that monitors my GPIO (a push to talk button) and send "CTRL + 1" if the button is pressed. Mumble is listening to that combination and starts broadcasting when it is pressed.

I must run run my python script as root to be able to access the GPIO so I have added these lines to /etc/profile (so that root can access X):

export DISPLAY=:1.0
export XAUTHORITY=/home/icuser/.Xauthority

As said this works perfectly when executed with sudo:

sudo python /home/icuser/sendptt_zero.py

but when I execute my script with (@reboot in crontab):

sudo /usr/bin/python /home/icuser/sendptt_zero.py >> /home/hallgren/ic.log 2>&1 &

I get this in my ic.log file (when I press the gpio button that starts the emulate keybord key function in python (I'm using http://www.autopy.org/)):

No protocol specified
Could not open main display

My python script also has this line (won't work without it):

os.environ['DISPLAY'] = ':1.0'

Any ideas on how to get it to start X automatically and why it works from command line with sudo but not when started from crontab?

marked as duplicate by roaima, techraf, Jeff Schaller, Eric Renouf, Satō Katsura Nov 28 '16 at 12:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Cron doesn't use /etc/profile.

Write the variables at the top of your crontab file.

  • Thanks for the very fast answer! I added export DISPLAY=:1.0 and export XAUTHORITY=/home/icuser/.Xauthority to my crontab and now I only get Could not open main display. An improvement but still not there... – nickehallgren Nov 28 '16 at 10:16
  • That's because it takes time for Xorg to be fully started, so put a some seconds sleep before trying, or write a loop which tries more times to connect to X server. – Ipor Sircer Nov 28 '16 at 10:20
  • OK, I've now tried with sleep up to 30 seconds before starting my python script and I still get Could not open main display. I have a X11vnc running and I can see that Xorg is started long before the python script is started. – nickehallgren Nov 28 '16 at 10:32
  • Putting your python command into ~/.xinitrc is not option? – Ipor Sircer Nov 28 '16 at 10:34
  • Why not, but how do I do that? :) I tried to add exec sudo /usr/bin/python /home/... in .xinitrc but that is not the righ way as it didn't start... – nickehallgren Nov 28 '16 at 10:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.