Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions linux/usage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 58 additions & 0 deletions linux/usage/systemd.md
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing, you probably need to explain that service you are starting is, in this example, a Python program, but that you could have any sort of application start up here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, that makes sense.

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`.