Many users want their Raspberry Pi to perform tasks unattended, as part of automating the Raspberry Pi. This cuts down on repetitive tasks that usually fall on the administrator’s lap. A common way to trigger automatic events is when the Raspberry Pi first boots up. Such a configuration allows us to start a Python script automatically every time the Raspberry Pi first boots up.
In this tutorial, you will learn a couple of different ways to auto start a Python script on a Raspberry Pi when the device starts up. This will be facilitated by either crontab or systemd timers, both of which are installed by default on Raspberry Pi OS, so you will not be required to download or install any extra applications or services.
In this tutorial you will learn:
- How to edit crontab to run a Python script at system start up
- How to create a systemd timer to run a Python script at system boot
|Category||Requirements, Conventions or Software Version Used|
|Software||crontab, systemd, Python|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Auto start Python script via crontab on Raspberry Pi
The first method we will go over is probably the easier of the two, since it only takes a simple edit in the crontab file to set up our Python script to get executed automatically whenever the Raspberry Pi starts up.
Our options with
crontabare a little sparse when compared to the systemd method, so if you need more flexibility than what is offered by cron, you can proceed to that section below.
- Start by opening a command line terminal and executing the
crontabcommand. Note that each user has their own crontab file, so be sure to open the editor with the user that you want to be executing the Python script as.
$ crontab -e
- If this is your first time opening crontab, you will be asked which editor you want to use. The simplest one to use would be nano, so select this option if in doubt.
- Use the following syntax to add your Python script that you want cron to execute at every reboot. We are using the
@rebootdeclaration to indicate that we want the Python script executed any time the Raspberry Pi is booted up:
Save your changes and exit the file when done.
Auto start Python script via systemd on Raspberry Pi
We can also use the Raspberry Pi’s systemd (the default init system) to create a service file that gets executed upon boot.
- First, create a Systemd service file as in the example below. We will store this file as
[Unit] After=network.target [Service] ExecStart=/usr/local/bin/my-python-script.py [Install] WantedBy=default.target
After: Instructs systemd on when the Python script should be run. In our case the Python script will run after network connection. Other example could be mysql.target etc.
ExecStart: This field provides a full path to the actual script to be executed on startup
WantedBy: Into what boot target the systemd unit should be installed
- Make sure you have created the Python script that your new systemd timer refers to above. Then, apply the appropriate permissions to the file so that systemd can execute it. Afterwards, we will apply the proper permissions to the systemd timer file.
$ sudo chmod 744 /usr/local/bin/my-python-script.py $ sudo chmod 664 /etc/systemd/system/python-script.service
- Next, enable the service unit:
$ sudo systemctl daemon-reload $ sudo systemctl enable python-script.service
All done. You should see that your Python script was executed upon the next reboot of your Raspberry Pi, as well as all subsequent system boots.
In this tutorial, we saw how to auto start a Python script on startup on a Raspberry Pi system. There are two main ways to configure this setup, which involve either cron or making a systemd timer. Both have their own additional tweaks that can be made, so one might work better than the other, depending on your situation and the script that you want to execute. Both are pretty easy to put into place, so feel free to try both and see which one works best for you.