So you've got a Raspberry Pi and a Python script ready to go and you would like to run the script when your Raspberry Pi boots. Maybe it's a Raspberry Pi security camera system, a bird box or OpenCV robot, all which need to run without a monitor and immediately start doing something.
There are many ways to get a script to run after a Raspberry Pi has booted, here we'll go through a GUI driven method using Gnome Schedule and get a script to run when the Raspberry Pi boots.
This tutorial works best for simple python scripts which aren't likely to crash, things like light scripts for the Pumpkin Pi
or Christmas Tree
so they light up on boot. There are other methods available, such as using systemd, for recovering more weighty applications which might fail during runtime and need a reboot.
Scheduling events using Gnome-Schedule
Setting up Gnome Schedule
1) Open a terminal window from the menu bar.
2) In the terminal window type:
sudo apt-get install gnome-schedule
And hit enter. Your Pi may ask you if you want to use memory to download this. Type "Y" and hit enter.
3) Once this has downloaded check that we can load Gnome Schedule by clicking on the Raspberry Pi in the top left corner, clicking on "System Tools" and then "Scheduled Tasks".
Building a test script
Firstly we need an example script for the scheduler to run. So lets build a print statement which prints a nice message when the script is run.
1) Open the file manager by clicking on the icon on the start bar.
2) Inside /home/pi create a new file by right clicking in the folder and selecting "Create New..." then "Empty File". Name the file runonboot.py.
3) Right click on the file and open it inside IDLE (or your preferred IDE).
4) Add the line
print('I\'ve run my little script now, thank you.')
And hit F5 to save and run the script.
5) This should print to the screen 'I've run my little script now, thank you."
Now we've got a script which prints out a short message when it's run. Next we need to set up Gnome Schedule.
Creating a scheduled task
Now we need to set up a task that will be run when the Raspberry Pi boots.
1) Click on "New", then "A task that launches recurrently"
2) Description should be set to "Print my words".
3) Command should be set to "sudo python /home/pi/runonboot.py >/home/pi/Desktop/log.txt"(these paths need to be absolute).
The first path indicates the file to be run, and the path after the ">" is where a text file will be written to when the Python script is run so we can see that it has actually been run.
4) Under Time & Date make sure Basic is set to "At reboot".
5) Click "Add" and then "Ok" to the warning that pops up.
We now need to reboot our Pi and check that the script has run.
Rebooting the Pi
1) Open a terminal and type:
2) On reboot there should be a text file called "log.txt" on the Desktop which contains the words "I've run my little script now, thank you."
Now you can replace "runonboot.py" with your own script that needs to run on boot. Have fun, and let us know in the comments what you've got running on boot with your Raspberry Pi!