From 3fd8ffffbf7c06743cf53121a59117bd793b5dc6 Mon Sep 17 00:00:00 2001 From: buxtonpaul Date: Thu, 29 Jun 2017 13:31:08 +0100 Subject: [PATCH 1/7] Info on creating a systemd control file --- linux/usage/systemd.md | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 linux/usage/systemd.md diff --git a/linux/usage/systemd.md b/linux/usage/systemd.md new file mode 100644 index 000000000..e150aacbf --- /dev/null +++ b/linux/usage/systemd.md @@ -0,0 +1,59 @@ +# 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 e.g. + +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 python3 from our working directory /home/pi/myscript which contains our python program to run myscript.py + +Copy this file into /lib/systemd/system as root e.g. +``` +sudo cp myscript.service /lib/systemd/system/myscript.service +``` + +Once this has been copied you can attempt to start the service using +``` +sudo systemctl start myscript.service +``` + +and stop it using +``` +sudo systemctl stop myscript.service +``` +When you are happy that this starts and stops your app, then you can have it start automatically on reboot by using +``` +sudo systemctl enable myscript.service +``` + +The systemctl command can also be used to restart the service or disable it from boot up as well! + +Some things to be aware of: +The order of when things are started is based on their dependancies, this particular script should start fairly late in the boot process, after a network is available (see the After section). +You can configure different dependancies and orders based on your requirements. + + +You can get more information from +``` man systemctl``` +or from https://fedoramagazine.org/what-is-an-init-system/ + +Also, be sure to reference absolute filenames rather than relative to your home folder; for example, `/home/pi/myscript.py` rather than `myscript.py`. From 4cdbd74de7c40b2aae55b1d5d10f2428c45e0360 Mon Sep 17 00:00:00 2001 From: buxtonpaul Date: Thu, 29 Jun 2017 13:34:28 +0100 Subject: [PATCH 2/7] Added systemd control Added systemd service configuration guide. --- linux/usage/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linux/usage/README.md b/linux/usage/README.md index 2f0635e54..7a3cf78fa 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 bootup. - [rc.local](rc-local.md) - Configuration of initialisation From 8dfe781a43a7ce9b722f05c496d2b1695c2bbeed Mon Sep 17 00:00:00 2001 From: buxtonpaul Date: Tue, 11 Jul 2017 10:11:51 +0100 Subject: [PATCH 3/7] Update systemd.md --- linux/usage/systemd.md | 1 + 1 file changed, 1 insertion(+) diff --git a/linux/usage/systemd.md b/linux/usage/systemd.md index e150aacbf..c8c3e7641 100644 --- a/linux/usage/systemd.md +++ b/linux/usage/systemd.md @@ -25,6 +25,7 @@ User=pi WantedBy=multi-user.target ``` So in this instance the service would run python3 from our working directory /home/pi/myscript which contains our python program to run myscript.py +In this case we are running a python3 script but you could run anything you like here. Copy this file into /lib/systemd/system as root e.g. ``` From 4f853c803c5929c55c0b644908b2cd011580abbc Mon Sep 17 00:00:00 2001 From: buxtonpaul Date: Tue, 11 Jul 2017 10:14:35 +0100 Subject: [PATCH 4/7] Update systemd.md Expanded description of the ExecStart line. --- linux/usage/systemd.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/linux/usage/systemd.md b/linux/usage/systemd.md index c8c3e7641..156f38db4 100644 --- a/linux/usage/systemd.md +++ b/linux/usage/systemd.md @@ -24,8 +24,7 @@ User=pi [Install] WantedBy=multi-user.target ``` -So in this instance the service would run python3 from our working directory /home/pi/myscript which contains our python program to run myscript.py -In this case we are running a python3 script but you could run anything you like here. +So in this instance the service would run python3 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 startup. Copy this file into /lib/systemd/system as root e.g. ``` From 73aac404695aaab065847c08da2f54aaa01f7100 Mon Sep 17 00:00:00 2001 From: JaninaPi Date: Thu, 14 Sep 2017 16:26:38 +0100 Subject: [PATCH 5/7] copy edits --- linux/usage/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/usage/README.md b/linux/usage/README.md index 7a3cf78fa..2f7ac359c 100644 --- a/linux/usage/README.md +++ b/linux/usage/README.md @@ -17,6 +17,6 @@ Some general help with Linux usage. - [.bashrc and .bash_aliases](bashrc.md) - Your shell configuration and aliases - [systemd](systemd.md) - - Configuration of systemd services to start scripts at bootup. + - Configuration of systemd services to start scripts at booting - [rc.local](rc-local.md) - Configuration of initialisation From cbddc262c47a581d777564b663f3b099516fbf80 Mon Sep 17 00:00:00 2001 From: JaninaPi Date: Thu, 14 Sep 2017 16:28:02 +0100 Subject: [PATCH 6/7] copy edits --- linux/usage/systemd.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/linux/usage/systemd.md b/linux/usage/systemd.md index 156f38db4..69ce66e1d 100644 --- a/linux/usage/systemd.md +++ b/linux/usage/systemd.md @@ -1,11 +1,10 @@ # 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 +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 e.g. +On your Pi, create a .service file for your service, for example: myscript.service From c76663caca950910ace4350144eeec527af2b68b Mon Sep 17 00:00:00 2001 From: JaninaPi Date: Thu, 14 Sep 2017 16:32:24 +0100 Subject: [PATCH 7/7] copy edits --- linux/usage/systemd.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/linux/usage/systemd.md b/linux/usage/systemd.md index 69ce66e1d..5827107ff 100644 --- a/linux/usage/systemd.md +++ b/linux/usage/systemd.md @@ -23,36 +23,36 @@ User=pi [Install] WantedBy=multi-user.target ``` -So in this instance the service would run python3 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 startup. +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 e.g. +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 +Once this has been copied, you can attempt to start the service using the following command: ``` sudo systemctl start myscript.service ``` -and stop it using +Stop it using following command: ``` sudo systemctl stop myscript.service ``` -When you are happy that this starts and stops your app, then you can have it start automatically on reboot by using +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 as well! +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 of when things are started is based on their dependancies, this particular script should start fairly late in the boot process, after a network is available (see the After section). -You can configure different dependancies and orders based on your requirements. ++ 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 +You can get more information from: ``` man systemctl``` -or from https://fedoramagazine.org/what-is-an-init-system/ +or here: https://fedoramagazine.org/what-is-an-init-system/ -Also, be sure to reference absolute filenames rather than relative to your home folder; for example, `/home/pi/myscript.py` rather than `myscript.py`. +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`.