Tested on:RPi 3 with Raspbian Jessie, but works for most unix systems
Prerequisites:Raspberry Pi with Raspbian Jessie(see steps 1-5 in the Introduction)
Hardware:LED with resistor and cables
4

Autostart Raspberry Pi programs: systemd, init.d, .bashrc and cron

There are different ways for autostarting Raspberry Pi programs. This tutorial will show how to autostart a program on the Raspberry Pi with .bashrc, init.d or a cron job.

Update
Since this article was written, a lot has changed with autostarting programs on RaspiOS. rc.local only works sparingly, since not all is loaded when rc.local is called. .bashrc is not very comfortable, since opening a terminal starts the program a second time. The way to go for now is using systemd for programs that do not use the desktop (could be Django). How to autostart with systemd is shown at the end of this tutorial.

Create dummy python script
For visualising the autostart, attach an LED to pin 11 in BOARD numbering.
Create a file test.py with

and fill it with

This script can be autostarted with all of the following options.

.bashrc commands
The .bashrc is a bunch of code that is loaded when a terminal opens (alternatively, commands in bash_profile are executed just once every terminal session).
Open the .bashrc with

then add at the end of the file:

init.d scripts
init.d is a directory for demons: household scripts that are started on boot.
Create a file autostart.sh with

and fill the file with

Save the file, then make the script executable and move it to the appropriate location:

For deactivation, move autostart.sh out of init.d.

Cron job
Cron is a job scheduling system that can start programs at given times or events.
Enter

Select the editor nano, then add at the bottom of the file

systemd
Enter

Fill the file with

Save and exit, then enter

Links
Django without desktop: http://www.knight-of-pi.org/raspberry-pi-django-setup-without-loading-the-desktop/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

4 Comments

  1. Appreciate this info on autostarting Raspberry Pi programs. I am planning to use the laddergame at our fall festival next week. I’m not sure about the electricity so I plan to use portable charging device to power rpi. It works well. So I set up the game to start on boot, what is the correct way to stop the game? Currently I’m testing things out and all I was able to do was pull the plug.

    • Sorry for being way too late for your festival. An easy way to support safe shutdown would be to add another physical button, which, if pressed, performs a shutdown. This could be implemented in the play loop of the ladder game or with a seperate button listener.

    • Totally missed your post, sorry.
      rc.local can execute shell scripts like .bashrc, but is only called once after bootup, not for every created shell.
      init.d scripts is for daemons ect. and scripts in there should have some special formatting, so .bashrc or rc.local are usually best…

Comment

Time limit is exhausted. Please reload CAPTCHA.