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

Systemd: Difference between revisions

From Nest Guides
Samuel (talk | contribs)
Fix a couple formatting and clarity issues
m oops
Tags: Manual revert Visual edit
 
(8 intermediate revisions by 5 users not shown)
Line 3: Line 3:
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 <code>--user</code> 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 <code>--user</code> flag.
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 <code>--user</code> 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 <code>--user</code> flag.


To get started, you need to create a systemd service file in the <code>~/.config/systemd/user/</code> directory, named <code><name>.service</code> (with <code>name</code> being the name of your app). This file should be setup as follows:<syntaxhighlight lang="ini">
For more info, take a look at the [https://wiki.archlinux.org/title/Systemd/User Arch Wiki] or https://linuxhandbook.com/create-systemd-services/ .
 
The man page for systemd.service can be found [https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html 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 <code>~/.config/systemd/user/<name>.service</code>:<syntaxhighlight lang="systemd">
[Unit]
[Unit]
Description=
Description=My amazing service
DefaultDependencies=no
 
After=network-online.target
# 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]
[Service]
Type=oneshot
# ExecStart is relative to this directory!
ExecStart=
WorkingDirectory=%h/my-service
TimeoutStartSec=0
 
# 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]
[Install]
WantedBy=default.target
WantedBy=default.target
</syntaxhighlight>Let's say you've modified the above template and saved it in <code>~/.config/systemd/user/my-service.service</code>.
First, reload your user systemd so that it picks up the new service:<syntaxhighlight lang="shell">
systemctl --user daemon-reload
</syntaxhighlight>Next, enable the service so that it automatically runs on startup. Here, the <code>--now</code> flag is also passed, which starts the service as well:<syntaxhighlight lang="shell">
systemctl --user enable --now my-service.service
</syntaxhighlight>At this point, your service should now be enabled (on startup) and running. To confirm, check the status of your service:<syntaxhighlight lang="shell">
systemctl --user status my-service.service
</syntaxhighlight>This should print something similar to:<syntaxhighlight>
● 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
</syntaxhighlight>
</syntaxhighlight>


* <code>Description</code> is the title of the systemd service.
=== More commands ===
* <code>ExecStart</code> should be set to the path of the executable the service will be running.
These are all the basic commands that you can use to manage your Systemd services:
* <code>After</code> and <code>WantedBy</code> in this setup configure the service to run every time nest starts.
 
<code>systemctl --user status <name></code>: see status and recent logs of a service
 
<code>journalctl --user -xeu <name></code>: see all logs of a service
 
<code>systemctl --user start <name></code>: starts the service
 
<code>systemctl --user stop <name></code>: stops the service


Once you are done editing the configuration, save it and then run <code>systemctl --user daemon-reload</code>, <code>systemctl --user enable <name></code>, and <code>systemctl --user start <name></code>. This last command may appear to freeze. That is okay. Press CTRL + C and then check the status of the service with <code>systemctl --user status <name></code>. Your service should now be running!
<code>systemctl --user enable <name></code>: enables (makes it start when Nest starts) the service. Optionally takes a <code>--now</code> argument to start it as well.


For more info, take a look at https://linuxhandbook.com/create-systemd-services/
<code>systemctl --user disable <name></code>: disables (makes it not start when Nest starts) the service. Optionally takes a <code>--now</code> argument to stop it as well.

Latest revision as of 19:33, 25 June 2025

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.