systemd is the init system on every modern Linux. A service unit = .service file describing how to start/stop/restart a daemon. Replaces supervisord, pm2 (for Node), rc.d. Auto-start on boot, auto-restart on crash, journalctl for logs, systemctl for control. Minimal unit — 10 lines.
Below: step-by-step, working examples, common pitfalls, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
systemctl daemon-reload — reload unitssystemctl enable myapp — auto-start on bootsystemctl start myapp + systemctl statusjournalctl -u myapp -f (live tail)| Scenario | Config |
|---|---|
| Simple Node.js service | [Unit]
Description=My App
After=network.target
[Service]
Type=simple
User=nodejs
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target |
| Python with venv | [Service]
Type=simple
User=app
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/venv/bin/python /opt/myapp/app.py
Environment="PATH=/opt/myapp/venv/bin"
Restart=on-failure |
| Systemd hardening (sandboxing) | [Service]
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/var/log/myapp |
| Timer (cron-like) | # /etc/systemd/system/backup.timer
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target |
| Reload config without restart | ExecReload=/bin/kill -HUP $MAINPID
# systemctl reload myapp |
Environment="KEY=value", not bash syntaxuseradd -r -s /bin/false nodejsjournalctl -u myappsystemctl daemon-reload after a unit change → stale configTo create a systemd service, start by defining a service unit file in the /etc/systemd/system/ directory, using the .service extension. Specify the service's behavior with directives like Unit, Service, and Install. After saving the file, enable and start the service using systemctl enable your-service-name and systemctl start your-service-name.
Creating a systemd service is a fundamental skill for managing system processes efficiently. Here’s a step-by-step guide to help you set up your own service.
A systemd service unit file typically consists of three main sections:
Start by creating a new service file in the /etc/systemd/system/ directory. Use a descriptive name for your service, following the naming convention your-service-name.service. For example:
sudo nano /etc/systemd/system/my-example.serviceIn the [Unit] section, you can specify the description and any dependencies. Here’s an example:
[Unit]
Description=My Example Service
After=network.targetThe [Service] section is crucial as it defines how the service behaves. Here’s an example configuration:
[Service]
Type=simple
ExecStart=/usr/bin/python3 /path/to/your_script.py
Restart=on-failure
User=nobody
Group=nogroupIn this example:
ExecStart command is executed.The [Install] section specifies how the service should be enabled or disabled. Here’s a simple example:
[Install]
WantedBy=multi-user.targetThis indicates that the service should start during the boot process when reaching the multi-user target.
After defining the sections, save the file and exit the text editor. In nano, you would do this by pressing CTRL + X, then Y to confirm saving, followed by Enter.
To recognize the new service file, reload the systemd manager configuration with the following command:
sudo systemctl daemon-reloadTo enable the service to start at boot, run:
sudo systemctl enable my-example.serviceNow, start your service with the command:
sudo systemctl start my-example.serviceCheck the status of your service to ensure it’s running correctly:
sudo systemctl status my-example.serviceThis command will show you if the service is active and running, along with any logs if there are issues.
You can stop, restart, or disable the service using the following commands:
sudo systemctl stop my-example.servicesudo systemctl restart my-example.servicesudo systemctl disable my-example.serviceIf your service does not behave as expected, check the logs for errors. You can do this using:
journalctl -u my-example.serviceBy following these steps, you can create a robust systemd service tailored to your application's needs, improving your system's reliability and efficiency.
systemd — built-in Linux, zero deps. pm2 — specialized for Node.js (cluster, zero-downtime reload). For simple apps — systemd.
on-failure (restart on non-zero exit) — standard. always — restart even on normal exit (useful for daemons). no — manual only.
<code>TimeoutStopSec=30s</code> — systemd waits 30 s before SIGKILL. App must handle SIGTERM and exit cleanly.
journald auto-rotates. Settings: <code>/etc/systemd/journald.conf</code> → SystemMaxUse=1G, MaxRetentionSec=30d.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.