RasPi Banner

Introduction

A common use-case is to autorun a Python script on your Raspberry Pi. Needless to say, this works on basically any Linux distro and/or configuration, not only on your Pi. There are many tutorials out there to achieve this, some can be found in the Links section. We will use a common one - setting up an Autorun script via crontab.

Crontab

A Crontab is a simple text file with a list of commands meant to be run at specified times. It is edited with a command-line utility. These commands (and their run times) are then controlled by the cron daemon, which executes them in the system background.

Crontab is a table used by cron which is a tool (deamon) used to run specific commands at a particular time. This mechanism is very flexible: you can run any number of programs at boot time (this is what we are going to do), you can run a script at a given time every day etc. Options will be outlined later on.

NOTE: While this tutorial is intended for our Raspberry Pi, nothing can stop us to use the same approach on most if not all Linux systems.

Autorun Python Script

We will explore the possibility of running a python script with crontab. It has many options for timing commands, one of them being reboot. This includes both startup of your device and as the name suggests rebooting of your device.

NOTE: Once you run crontab to edit the options for the first time, you will be first asked to choose an editor. As we work headless and want things simple, we will choose nano. Obviously if you are happy with a different editor, go ahead and choose it instead.

Let us assume we want to run our python program mycode.py.

  • Write down the location of your program: for instance /home/pi/mycode.py
  • launch the following command to edit the crontab configuration, as root:
sudo crontab -e
  • add a new entry at the bottom of the file (reboot option), indicating the location of your program noted earlier:
@reboot sudo python /home/pi/mycode.py
  • Save and exit, then reboot

NOTE: at this point in time, your script should be running after reboot. Chances are that something went wrong and you need to debug the code, maybe checking the output. It is a good idea to autorun the script and redirect output to a file you can check. So instead of the above entry, you can try the following:

@reboot sudo python /home/pi/mycode.py > /home/pi/mycode-log.txt

NOTE: when debugging potential errors, it is also a good idea to look at the system log. You can achieve this by invoking

grep cron /var/log/syslog

NOTE: sometimes, even after debugging, the script doesn't run even though the individual components work great. Don't give up. There are situations when the cron line simply 'kicks in' too early (before other dependancies are loaded). The below invoking of your script is a good way to eliminate this type of situation. Basically wait a bit, then run your code.

@reboot sleep 60 && sudo python /home/pi/mycode.py

Always read more Python related topics on our main Python page.