Enabling a Python script to launch when booting a Raspberry Pi
Hello, this blog post was inspired by Dave Darch...
@biglesp @martinohanlon @LegoJames @guru What's best way to launch a .py file at boot, headless. Eg. drums.py from Explorerhat...systemd?
— Dave Darch (@davedarch) August 22, 2016
There are many ways to run a Python script at boot, but my personal go to solution for the Raspberry Pi is a three step process so here we go!
Step 1: Add a path to your Python code
A path is an instruction to your Python project, the instruction being the location of the Python interpreter that will run your code. The path is a line of code added to your Python project. This will be the first line of code in your project!
For Python 2
#! /usr/bin/env python
For Python 3
#! /usr/bin/env python3
Save your work. This will now instruct the Python code to look for the Python interpreter in your Raspbian operating system.
Step 2: Make the Python code executable
Now we need to exit the Python project and open LXTerminal, the icon for which is located in the top left of the Raspbian desktop, it looks like a little PC monitor.
In the terminal, navigate to where your Python project is located, for example if it is located in /home/pi/Python you would type
cd /home/pi/Python
Now that we are in the directory containing our Python code, we use the "chmod" command to modify the permissions of the file. To make it executable we do the following
chmod +x YourPythonProject.py
With that completed our final step approaches!
Step 3: Crontab FTW!
Crontab is a Linux method of scheduling something to happen at a certain time. It is really cool and an awesome tool to investigate.
Still in the Terminal we need to issue a command to open crontab.
crontab -e
This will most likely be your first time using crontab so you will see a mini menu pop up. This is a menu of suggested editors to edit the file. For ease accept the default which should be nano.
When crontab opens you will see lots of text, navigate to the bottom of the file and add a new line. We will now instruct crontab to launch our Python code on boot.
It looks like this, remember to change the /home/pi/Python/YourPythonProject.py to match where your file is located.
@reboot /home/pi/Python/YourPythonProject.py
Now press CTRL + X to exit, you will be prompted to save, press Y and then ENTER
Now reboot your Raspberry Pi and your code should autorun on boot :D
sudo reboot
Enjoy your new embedded Raspberry Pi!
Step 4: Optional - Change boot options to boot to a terminal
This is an optional step that was suggested by @Code_Tap
@biglesp @davedarch great stuff!! don't forget to set "sudo raspi-config" to boot to cli so you don't waste cpu on the gui :)
— Code_Tap (@Code_Tap) August 22, 2016
So as Code_Tap says, in the Terminal you can enter the command
sudo raspi-config
and in there will be an option to change the boot settings. For an embedded system booting to the desktop is a wasteful use of resources, as we will never see the desktop. So change it to boot to the terminal, save, exit and reboot. Your Pi will boot a little faster now, and the code will still run as normal.