I have a script very similar to the one at the end of the pygame example page, and I want to run it on startup to make the whole thing listen for commands on startup. I ran "crontab -e" and as a first line I put "@reboot python3 myscript.py". Upon rebooting, the script doesn't work, and returns pygame.error: video system not initialized. On the other hand, if I execute "python3 myscript.py" from the terminal, the script works.

The vast majority of answers simply notes that pygame.init() is not called, which is not the case here. According to a post translated from German, this can be solved by exporting a variable somewhere, but I am not sure where or how to do that.

Is there a way to give X11 privileges to the script ran on reboot by Crontab?

UPDATE: Upon chaining commands like Seamus suggested, setting DISPLAY=:0 doesn't work at all. From terminal after login, I did echo $DISPLAY and got :1. When setting:

@reboot (sleep 30; export DISPLAY=:1; python3 /full/path/to/myscript.py) 

it starts working after login, but not before (which is my main requirement, as the device should be autonomous). I am not sure if this :1 is relevant and what is :0.

  • I'm confused... you've said The script doesn't work..., but in the next sentence you've said, The script works fine when ran from terminal normally. Could you clear this up, please? – Seamus Apr 2 '19 at 20:22
  • It doesn't work when ran on startup by crontab, it works fine when I run it from CLI. I will try to edit the question to make it more clear. – Cindy Almighty Apr 2 '19 at 20:48
  • OK - that will help, but knowing that's the case I have an idea where the problem is. I'll post my answer momentarily. – Seamus Apr 2 '19 at 23:08

If your script runs from the command line, but not from cron, there are two usual suspects:

  1. The PATH for cron is different than your path as a "regular" user. You can address that issue by using a full path spec for your script:
@reboot python3 /full/path/to/myscript.py 
  1. cron is unaware of the status of any services when it starts. For cron, @reboot means when cron starts, not when the network is up, or any other services your cron job may require. One way to take care of this is to sleep for a bit before running your script:
@reboot (sleep 30; python3 /full/path/to/myscript.py)  

This will delay execution of your script by 30 seconds. Nothing magic about 30 seconds... it will depend upon your system. You may wish to do some trial and error testing to find a reliable sleep time.

Note that another approach that avoids cron's shortcomings wrt status of system services is to use systemd. Some (myself included) find cron easier to use, but certainly systemd has some advantages.

Finally, it's usually a good idea to collect any error messages generated when starting your script. Another addition to the single line in your crontab will accomplish that:

@reboot (sleep 30; python3 /full/path/to/myscript.py >> /home/yours/cronjoblog) 2>&1

Any error messages from stderr will be appended to the file you designate.

| improve this answer | |
  • Thank you for your answer. Uknowningly, I avoided both problems you are mentioning - I did use the full path and made an infinite loop of trying and logging - it reports the same error long after the system has started up. I also try the initialisation step over and over, so I feel like the right solution is passing the display variable. The only problem is I am not sure how. – Cindy Almighty Apr 3 '19 at 12:46
  • @CindyAlmighty: Sorry that didn't help. You might look into the ~/.Xauthority file for a solution? – Seamus Apr 3 '19 at 12:59
  • Unfortunately, I can't find the ~/.Xauthority file, or the environment variable. I looked at this post and I don't have the equivalent either - the path doesn't exist (I have /var/run/gdm3/greeter/...). – Cindy Almighty Apr 3 '19 at 14:17
  • Wish I could help more... I'm not a frequent user of X, but was under the impression that ~/.Xauthority was required. But it seems you've narrowed the problem down a bit. Maybe post another question focused on your edited question? – Seamus Apr 3 '19 at 18:35
  • True. This is solid advice for running Crontab. Cheers! – Cindy Almighty Apr 3 '19 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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