Anybody using Scheduled Tasks to run a python program on boot up? How did you do it? I having problems running a script from Scheduled Tasks but can start it in Terminal.

  • I run my Pi with Pixel and use Scheduled task under the System Tools, it there I’m having problems with scheduling a python program on reboot. – Johnuready Dec 7 at 9:36

By "Scheduled Tasks", I will assume you are speaking of cron. If my assumption is correct, you should first read this documentation, and then you should read man cron and man crontab. This will get you up to speed on the basics.

As for your question, you will use the @reboot facility in your crontab. If you're having trouble running it, but it runs OK from the command line, there are two possibilities:

  1. When you run a program from the command line, it runs in the environment (env) of the user (pi?) that invoked the program. That environment includes permissions, and a PATH (i.e. echo $PATH). When you run a program from cron, the program will NOT, in general, run in the same environment, and therefore the PATH will be different. The way to work around this is to use full path specs for all files and programs needed in your script.

  2. Scripts run under cron may fail unexpectedly when using the @reboot invocation) because cron maintains no awareness of whether or not the resources required to execute a command are available before attempting the execution. Redirecting stderr to a log file in your cron command may give you some insights on this - here's an example that writes stderr to a file named cronjoblog in user pi's home directory:

    @reboot /usr/bin/python3 ~/PiPyMailer/ > /home/pi/cronjoblog 2>&1

If the issue is that the resources needed aren't available when cron starts @reboot, add a sleep 30; command before your script. This will allow the system reboot to progress and likely complete before starting your script. You should experiment with the "sleep time" as 30 seconds may be overkill.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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