Crontab: How to Run a Simple Command When Your Raspberry Pi Boots

A simple method for running commands automatically on boot.
Zach Zach (213)
Total time: 2 minutes 

This short guide will show you how to execute a simple command automatically when your Raspberry Pi boots up. There are several methods for doing this, but for executing basic commands and running scripts without many dependencies, crontab is the easiest method.

This method is recommended for basic commands where you don't need to wait for specific system resources to become available.

Here's everything you'll need to complete this guide:

Raspberry PiRaspberry Pi×1

To run basic commands or auto-run programs on Raspbian boot, we'll use crontab. crontab can be used to run commands on boot or at a specific time interval.

First, connect to your Raspberry Pi.

Then, open crontab:

crontab -e

When prompted, I recommend choosing the Nano editor to edit the crontab.

At the bottom of the file, add the following @reboot entry:

@reboot [your command here]

Run a Python script on Raspberry Pi boot

For example, this command runs a Python script automatically on system boot:

@reboot python /home/pi/myscript.py

When you're done, save the file and exit.

To run a command containing sudo, you'll need to edit the root user's crontab instead:

sudo crontab -e

Then, you can do:

@reboot sudo python /home/pi/myscript.py

If your script isn't executing, check the system log for cron events:

grep cron /var/log/syslog