diff --git a/linux/usage/README.md b/linux/usage/README.md index 2f0635e54..2f7ac359c 100644 --- a/linux/usage/README.md +++ b/linux/usage/README.md @@ -16,5 +16,7 @@ Some general help with Linux usage. - Setting up scheduled tasks - [.bashrc and .bash_aliases](bashrc.md) - Your shell configuration and aliases +- [systemd](systemd.md) + - Configuration of systemd services to start scripts at booting - [rc.local](rc-local.md) - Configuration of initialisation diff --git a/linux/usage/systemd.md b/linux/usage/systemd.md new file mode 100644 index 000000000..5827107ff --- /dev/null +++ b/linux/usage/systemd.md @@ -0,0 +1,58 @@ +# systemd + +In order to have a command or program run when the Pi boots, you can add it as a service. Once this is done, you can start/stop enable/disable from the linux prompt. + +## Creating a service + +On your Pi, create a .service file for your service, for example: + +myscript.service + +```[Unit] +Description=My service +After=network.target + +[Service] +ExecStart=/usr/bin/python3 -u main.py +WorkingDirectory=/home/pi/myscript +StandardOutput=inherit +StandardError=inherit +Restart=always +User=pi + +[Install] +WantedBy=multi-user.target +``` +So in this instance, the service would run Python 3 from our working directory `/home/pi/myscript` which contains our python program to run `myscript.py`. But you are not limited to Python programs: simply change the ExecStart line to be the command to start any program/script that you want running from booting. + +Copy this file into `/lib/systemd/system` as root, for example: +``` +sudo cp myscript.service /lib/systemd/system/myscript.service +``` + +Once this has been copied, you can attempt to start the service using the following command: +``` +sudo systemctl start myscript.service +``` + +Stop it using following command: +``` +sudo systemctl stop myscript.service +``` +When you are happy that this starts and stops your app, you can have it start automatically on reboot by using this command: +``` +sudo systemctl enable myscript.service +``` + +The `systemctl` command can also be used to restart the service or disable it from boot up! + +Some things to be aware of: ++ The order in which things are started is based on their dependencies — this particular script should start fairly late in the boot process, after a network is available (see the After section). ++ You can configure different dependencies and orders based on your requirements. + + +You can get more information from: +``` man systemctl``` +or here: https://fedoramagazine.org/what-is-an-init-system/ + +Also be sure to reference absolute file names rather than doing so relative to your home folder, for example use `/home/pi/myscript.py` rather than `myscript.py`.