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 Bash 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 Bash 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 Bash script at startup
- How to create Systemd service unit
- How to create a simple disk space check script
- How to set permissions to Systemd service unit file
- How to set script permissions
- How to enable Systemd service unit to run at the system startup
|Requirements, Conventions or Software Version Used
|crontab, systemd (both installed by default)
|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 Bash 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 Bash script to get executed automatically whenever the Raspberry Pi starts up.
Our options with
crontab are 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 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 script or command that you want cron to execute at every reboot. We are using the
@rebootdeclaration to indicate that we want the script executed any time the Raspberry Pi is booted up:
Save your changes and exit the file when done.
Auto start Bash 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 an example below. We will store this file as
[Unit] After=network.target [Service] ExecStart=/usr/local/bin/disk-space-check.sh [Install] WantedBy=default.target
After: Instructs systemd on when the script should be run. In our case the 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
For more information on how to create Systemd service unit execute the
- Create a script to be executed on Raspberry Pi system startup. As specified in the above Step 1, the path and the name of the new script in our example will be
The below is an example of such script:
#!/bin/bash date > /root/disk_space_report.txt du -sh /home/ >> /root/disk_space_report.txt
- Set appropriate permissions for both, the Systemd service unit and script:
$ sudo chmod 744 /usr/local/bin/disk-space-check.sh $ sudo chmod 664 /etc/systemd/system/disk-space-check.service
- Next, enable the service unit:
$ sudo systemctl daemon-reload $ sudo systemctl enable disk-space-check.service
- Now you are ready to reboot your Raspberry Pi. Once the system boots you should see the following file containing disk space usage within your
$ sudo ls /root/ disk_space_report.txt
In this tutorial, we saw how to auto start a Bash 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.