0

Here's 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 haven't managed to start X on boot so I have a @reboot line in crontab that executes a script that includes this:

#!/bin/bash
while ! ping -c 1 -W 1 192.168.1.100; do
    sleep 1
done
/usr/bin/startx

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 I 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 keyboard 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'

Do you have 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?

karel
  • 2,030

1 Answers1

1

Cron doesn't use /etc/profile.

Write the variables at the top of your crontab file.

Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39
  • 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
  • skip exec, it's not a window manager. And add & at the of line. – Ipor Sircer Nov 28 '16 at 11:00
  • Now I understand why the python script is not executed. When I made the .xintrc file I can't start x with startx anymore. I just get xinit: `connection to X server lost

    waiting for X server to shut down (EE) Server terminated successfully (0). Closing log file.

    Couldn't get a file descriptor referring to the console`

    – nickehallgren Nov 28 '16 at 11:29
  • OK, now I got it to work. I added everything on the same line in my bash script like DISPLAY=:1.0 XAUTHORITY=/home/icuser/.Xauthority sudo /usr/bin/python /home/icuser... – nickehallgren Nov 28 '16 at 12:39