sudo nano /etc/systemd/system/myapp.serviceCustom local units usually belong under /etc/systemd/system.
Author, edit, override, and harden systemd .service files with practical examples.
Create and install custom service units.
sudo nano /etc/systemd/system/myapp.serviceCustom local units usually belong under /etc/systemd/system.
[Unit]
Description=My App
After=network.target
[Service]
Type=simple
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.targetA minimal but production-useful service unit.
sudo systemctl daemon-reloadRequired after adding or editing unit files.
sudo systemctl enable --now myapp.serviceCommon final step after authoring a unit.
Override vendor unit settings safely.
sudo systemctl edit nginxCreates a drop-in under /etc/systemd/system/<unit>.d/override.conf.
[Service]
Environment=NODE_ENV=production
Environment=PORT=8080Drop-ins are the preferred way to add or change settings.
[Service]
ExecStart=
ExecStart=/usr/local/bin/myapp --config /etc/myapp/config.ymlWhen overriding ExecStart, the original must often be cleared first.
systemctl cat myapp.serviceHelps verify the effective configuration.
Set service identity and startup relationships.
[Service]
EnvironmentFile=/etc/myapp/myapp.envGood for keeping secrets and config out of ExecStart.
[Service]
User=myapp
Group=myappPreferred for least privilege.
[Service]
WorkingDirectory=/opt/myappUseful for relative file access and app startup.
[Unit]
Wants=network-online.target
After=network-online.targetCommon for network-dependent apps.
[Unit]
Requires=postgresql.service
After=postgresql.serviceStronger relationship than Wants.
Tune service behavior and isolation.
[Service]
Restart=on-failure
RestartSec=5Common production resilience setting.
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup.sh
RemainAfterExit=yesOften paired with timers.
[Service]
Type=notify
NotifyAccess=mainUsed by daemons with sd_notify support.
[Service]
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/myappA starting point for service hardening.
[Service]
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICEUseful when binding privileged ports without full root access.
systemd-analyze security myapp.serviceGood final review after adding sandbox settings.