How to autostart Bash script on startup on Raspberry Pi

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
How to autostart Bash script on startup on Raspberry Pi
How to autostart Bash script on startup on Raspberry Pi
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software crontab, systemd (both installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – 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.
  1. Start by opening a command line terminal and executing the crontab command. 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
  2. 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.
    Selecting a default text editor to use with crontab
    Selecting a default text editor to use with crontab
  3. Use the following syntax to add your script or command that you want cron to execute at every reboot. We are using the @reboot declaration to indicate that we want the script executed any time the Raspberry Pi is booted up:
    @reboot /home/linuxconfig/

    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.

  1. First, create a Systemd service file as in an example below. We will store this file as /etc/systemd/system/disk-space-check.service.

    After: Instructs systemd on when the script should be run. In our case the script will run after network connection. Other example could be 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 man systemd.unit command.
  2. 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 /usr/local/bin/
    The below is an example of such script:

    date > /root/disk_space_report.txt
    du -sh /home/ >> /root/disk_space_report.txt
  3. Set appropriate permissions for both, the Systemd service unit and script:
    $ sudo chmod 744 /usr/local/bin/
    $ sudo chmod 664 /etc/systemd/system/disk-space-check.service
  4. Next, enable the service unit:
    $ sudo systemctl daemon-reload
    $ sudo systemctl enable disk-space-check.service
  5. 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 /root directory:
    $ sudo ls /root/

Closing Thoughts

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.

Comments and Discussions
Linux Forum