To run basic commands or auto-run programs on Raspberry Pi OS (formerly 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:
When prompted, I recommend choosing the Nano editor to edit the crontab.
At the bottom of the file, add the following
@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