Setting Up Systemd Units on Linux

Linux

About Systemd

Refer to Ruan Yifeng’s blog post: “Systemd Tutorial: Commands”

Link: http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

Cheat Sheet for Unit Configuration

  1. Create a xxx.service file under /etc/systemd/system/
Terminal window
vim /etc/systemd/system/xxx.service
  1. A simple template for xxx.service content, where ExecStart should point to the actual executable or program launch command:
Terminal window
[Unit]
Description=xxx
Documentation=https://github.com/xxxx/xxxx
Wants=network.target
After=network.target
[Service]
Type=simple
DynamicUser=yes
ExecStart=/path/to/your/app/exec
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
  1. Register and use:
Terminal window
# Load the xxx service
systemctl daemon-reload
# Enable xxx to start on boot
systemctl enable xxx.service
# Check xxx status
systemctl status xxx.service
# Start xxx
systemctl start xxx.service
# Restart xxx
systemctl restart xxx.service
# Stop xxx
systemctl stop xxx.service