bashintermediate

Systemd Service File for Node.js

Systemd unit file to run a Node.js app as a managed service with auto-restart and logging.

bash
# /etc/systemd/system/webapp.service
[Unit]
Description=Web Application
After=network.target postgresql.service
Wants=postgresql.service

[Service]
Type=simple
User=webapp
Group=webapp
WorkingDirectory=/opt/webapp
ExecStart=/usr/bin/node dist/server.js
Restart=on-failure
RestartSec=5
StartLimitBurst=5
StartLimitIntervalSec=60

Environment=NODE_ENV=production
Environment=PORT=3000
EnvironmentFile=/opt/webapp/.env

StandardOutput=journal
StandardError=journal
SyslogIdentifier=webapp

LimitNOFILE=65535
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target

# Usage:
# sudo systemctl daemon-reload
# sudo systemctl enable webapp
# sudo systemctl start webapp
# sudo journalctl -u webapp -f

Use Cases

  • Running production services on Linux servers
  • Auto-restart on crash or reboot
  • Centralized logging via journald

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.