There will come a time when you have developed an awesome IoT project and you want it to run automatically every time you start your Raspberry Pi. Here is one simple way to achieve this using a feature of cron, the Unix scheduler. If you are not familiar with the basics of cron, search the web for cron tutorial—you'll find heaps of them. I've provided curated links in the Further reading section.
Here are the steps to configure cron and make a script run on boot:
- In your project folder, create a bash script. I've named it run_on_boot.sh:
#!/bin/bash
# Absolute path to virtual environment python interpreter
PYTHON=/home/pi/pyiot/chapter01/venv/bin/python
# Absolute path to Python script
SCRIPT=/home/pi/pyiot/chapter01/gpio_pkg_check.py
# Absolute path to output log file
LOG=/home/pi/pyiot/chapter01/gpio_pkg_check.log
echo -e "\n####### STARTUP $(date) ######\n" >> $LOG
$PYTHON $SCRIPT >> $LOG 2>&1
This bash script will run a Python script using the absolute paths for both the script and its Python interpreter. Also, it captures any script output and stores it in a log file. For this example, we're simply going to run and log the output of gpio_pkg_check.py on boot. It's the last line that ties everything together and runs and logs our Python script. The 2>&1 part at the end is necessary to ensure that errors, in addition to standard output, are also logged.
- Mark the run_on_boot.sh file as an executable file:
$ chmod u+x run_on_boot.sh
If you are not familiar with the chmod command (chmod means change mode), what we are doing is giving the operating system permission to execute the run_on_boot.sh file. The u+x parameters mean for the current User, make the file eXecutable. To learn more about chmod, you can type chmod --help or man chmod in the Terminal.
- Edit your crontab file, which is the file where cron scheduling rules are stored:
$ crontab -e
- Add the following entry to your crontab file, using the absolute path to the run_on_boot.sh bash script we created in step 1:
@reboot /home/pi/pyiot/chapter01/run_on_boot.sh &
Do not forget the &Â character at the end of the line. This makes sure the script runs in the background.
- Run the run_on_boot.sh file manually in a Terminal to make sure it works. The gpio_pkg_check.log file should be created and contains the output of the Python script:
$ ./run_on_boot.sh
$ cat gpio_pkg_check.log
####### STARTUP Fri 13 Sep 2019 03:59:58 PM AEST ######
GPIOZero Available
PiGPIO Available
- Reboot your Raspberry Pi:
$ sudo reboot
- Once your Raspberry Pi has finished restarting, the gpio_pkg_check.log file should now contain additional lines, indicating that the script did indeed run at boot:
$ cd ~/pyiot/chapter01
$ cat gpio_pkg_check.log
####### STARTUP Fri 13 Sep 2019 03:59:58 PM AEST ######
GPIOZero Available
PiGPIO Available
####### STARTUP Fri 13 Sep 2019 04:06:12 PM AEST ######
GPIOZero Available
PiGPIO Available
If you are not seeing the additional output in the gpio_pkg_check.log file after a reboot, double-check that the absolute path you entered in crontab is correct and that it works manually as per step 5. Also, review the system log file,  /var/log/syslog, and search for the text, run_on_boot.sh.
We have now learned alternative methods to run a Python script, which will help you in the future to correctly run your Python-based IoT projects after they are developed or start them when your Raspberry Pi boots if required.
Next, we will now move on to making sure your Raspberry Pi is set up and configured correctly for the GPIO and electronic interfacing that we'll be diving into in the next chapter, Chapter 2, Getting Started with Python and IoT, and subsequent chapters.