Back to articles
systemd for Node.js Developers — Auto-Restart, Logging, and 24/7 Uptime

systemd for Node.js Developers — Auto-Restart, Logging, and 24/7 Uptime

via Dev.to TutorialTateLyman

Why systemd? PM2, forever, and nodemon are great for development. But for production on a Linux VM, systemd is already there and does everything you need. The Service File # /etc/systemd/system/myapp.service [Unit] Description = My Node.js App After = network.target [Service] Type = simple User = ubuntu WorkingDirectory = /home/ubuntu/my-app ExecStart = /usr/bin/node app.js Restart = always RestartSec = 5 Environment = NODE_ENV=production StandardOutput = journal StandardError = journal [Install] WantedBy = multi-user.target Key Features Auto-Restart Restart=always + RestartSec=5 means if your app crashes, it restarts in 5 seconds. Logging journalctl -u myapp -f # Live logs journalctl -u myapp --since today # Today's logs journalctl -u myapp -n 100 # Last 100 lines No log rotation config needed — journald handles it. Commands sudo systemctl start myapp # Start sudo systemctl stop myapp # Stop sudo systemctl restart myapp # Restart sudo systemctl status myapp # Check status sudo systemc

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
3 views

Related Articles