From e5b720aadbaebabbd76ec3aecac21ddf643eb8c0 Mon Sep 17 00:00:00 2001 From: Shaun Setlock Date: Sun, 19 Jun 2022 16:11:44 -0400 Subject: [PATCH] Created the required files to setup systemd units for autostart. --- systemd/dht-start-restart'@pimonitoring1 | 1 + systemd/dht-start-restart@.service | 20 ++++++++++++++++++++ systemd/notes.md | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 systemd/dht-start-restart'@pimonitoring1 create mode 100644 systemd/dht-start-restart@.service create mode 100644 systemd/notes.md diff --git a/systemd/dht-start-restart'@pimonitoring1 b/systemd/dht-start-restart'@pimonitoring1 new file mode 100644 index 0000000..3d09941 --- /dev/null +++ b/systemd/dht-start-restart'@pimonitoring1 @@ -0,0 +1 @@ +PATH_TO_FILE=/home/pi/dht22/main.py diff --git a/systemd/dht-start-restart@.service b/systemd/dht-start-restart@.service new file mode 100644 index 0000000..a3a391a --- /dev/null +++ b/systemd/dht-start-restart@.service @@ -0,0 +1,20 @@ +# Unit file template for creating auto start and restart of python script for dht sensor. +# Template should be in /etc/systemd/system/dht-start-restart@.service +# Environment Variables for each instances should be at /etc/default/dht-start-restart@%i +# Enable with `systemctl enable --now remote-tunnel@%i` +# where %i is the name of the pimonitoring instance (e.g. pimonitoring1) +# +[Unit] +Description=Start python script for DHT22 sensor on %I +After=network.target + +[Service] +User=pi +EnvironmentFile=/etc/default/dht-start-restart@%i +ExecStart=/usr/bin/python3 ${PATH_TO_FILE} +RestartSec=15 +Restart=always +KillMode=mixed + +[Install] +WantedBy=multi-user.target diff --git a/systemd/notes.md b/systemd/notes.md new file mode 100644 index 0000000..fb061df --- /dev/null +++ b/systemd/notes.md @@ -0,0 +1,16 @@ +# Creating systemd unit for Auto Start/Restart +### The steps below outline the process for creating a systemd service unit. +### This service will automatically start the python script. + +## Procedure + +1. Create the service template by copying `dht-start-restart@.service` into `/etc/systemd/system/`. +2. Define environment variables for an instance of this template by copying `dht-start-restart@pimonitoring1` into `/etc/default/`. + - __NOTE__: The instance should replace the name of the RPiZW. (e.g. pimonitoring2, etc.) +3. Create the instance and enable it with `sudo systemctl enable --now dht-start-restart@pimonitoring1` + - __NOTE__: It may be required to reload the systemd daemon. `sudo systemctl reload-daemon` + +## Common Issues + - [ ] Is the `User=` in `dht-start-restart@.service` correct? pi vs. shaun + - [ ] Is the `{PATH_TO_FILE}` in `dht-start-restart@pimonitoring1` correct? `/home/pi` vs. `/home/shaun` + - [ ] Does the python script `main.py` contain any relative paths?