Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Systemd

From Nest Guides
Revision as of 19:25, 5 June 2025 by Isobel-p (talk | contribs) (Edit typo - change ./venv/ to /.venv/)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you want your apps to be integrated in the background with the system and start up when the system starts up, then you'll want to put your service with Systemd! Systemd is a system and service manager that runs as PID 1 and starts the rest of the system. It's used on most Linux distributions, including Debian, which Nest runs on!

Systemd is primarily used for starting the system itself, and these services are only accessible to the root user. However, it also works for users, with the --user flag when running any systemd command. Since Nest users don't have access to the root user, you'll need to remember to always use the --user flag.

For more info, take a look at the Arch Wiki or https://linuxhandbook.com/create-systemd-services/ .

The man page for systemd.service can be found here, which also contains links to additional man pages.

Tutorial

Here is a simple user service template that you can copy-paste and modify. This file should go in ~/.config/systemd/user/<name>.service:

[Unit]
Description=My amazing service

# Specifiers (%x) are documented in https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html#Specifiers
# %h = your home directory (/home/user) - use this instead of ~
# %u = your username (user)

[Service]
# ExecStart is relative to this directory!
WorkingDirectory=%h/my-service

# Uncomment if you want to set custom environment variables.
# Environment="COOL_VAR1=Thing1" "COOL_VAR2=Thing2 with spaces"
# It's better to use a dotenv library in your program instead of uncommenting this.
# EnvironmentFile=%h/my-service/.env

# Uncomment ONE of the following. This is the actual command your service will run.
# ExecStart=/.venv/bin/python myservice.py
# ExecStart=node server.js
# ExecStart=npm start
# ExecStart=./run.sh
# ExecStart=my-custom-command

# Restart the service automatically if it fails
Restart=on-failure

[Install]
WantedBy=default.target

Let's say you've modified the above template and saved it in ~/.config/systemd/user/my-service.service. First, reload your user systemd so that it picks up the new service:

systemctl --user daemon-reload

Next, enable the service so that it automatically runs on startup. Here, the --now flag is also passed, which starts the service as well:

systemctl --user enable --now my-service.service

At this point, your service should now be enabled (on startup) and running. To confirm, check the status of your service:

systemctl --user status my-service.service

This should print something similar to:

● my-service.service - My amazing service
     Loaded: loaded (/home/user/.config/systemd/user/my-service.service; enabled; preset: enabled)
     Active: active (running) since Sat 2025-03-08 22:54:25 UTC; 2 months 23 days ago
 Invocation: ead53deb778c4f2c9b9700aed7696e0c
   Main PID: 25332 (python3)
      Tasks: 78
     Memory: 30.7M (peak: 71.6M, swap: 23.3M, swap peak: 43M)
        CPU: 1h 40min 33.498s

More commands

These are all the basic commands that you can use to manage your Systemd services:

systemctl --user status <name>: see status and recent logs of a service

journalctl --user -xeu <name>: see all logs of a service

systemctl --user start <name>: starts the service

systemctl --user stop <name>: stops the service

systemctl --user enable <name>: enables (makes it start when Nest starts) the service. Optionally takes a --now argument to start it as well.

systemctl --user disable <name>: disables (makes it not start when Nest starts) the service. Optionally takes a --now argument to stop it as well.