Skip to content
Art edited this page Jan 9, 2017 · 1 revision

Overview of techniques

When you keep using your Pi for the same purpose time after time, you may decide you want to automatically start up a chosen program or script every time you turn on and Raspbian boots up.

Techniques you can use include:

  • Autologin with autorun script
    • uses inittab and profile
  • rclocal
    • a script in /etc/rc.local calls a script in your home folder
  • crontab

  • systemd

systemd

Also known as systemctl, this is a way of running your program or script as a Service. This typically gives you more control and flexibility than the other methods. In the past it might have been more difficult to set up, but recent Debian versions have made it much easier to use. In (Debian and) Raspbian Wheezy you had to create an init script in init.d with a whole load of bash commands including start-stop-daemon commands.

In Raspbian Jesse, however you simply need to define a Service Unit File, and register it.

# set up Unit file
sudo tee /etc/systemd/system/myService.service >/dev/null <<'EOF!'
[Unit]
Description=My Script Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/myScript.py

[Install]
WantedBy=multi-user.target
EOF!

#...with correct permissions
sudo chmod 644 /etc/systemd/system/myService.service

# then register it with systemd
sudo systemctl daemon-reload
sudo systemctl enable myService.service
  • credit - [http://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/]
  • help - [https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files]

Python code to stop service

Inside your python code you should include something called a Handler for the TERM(inate) Signal.

see also [https://blog.lanyonm.org/articles/2015/01/11/raspberry-pi-init-script-python.html#python-to-handle-the-term-signal]

others

see also:

Clone this wiki locally