Skip to main content

On This Page

Optimizing Node.js Production Uptime with systemd

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

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

systemd provides a native Linux solution for managing Node.js processes without third-party dependencies. The system ensures 24/7 uptime by implementing automatic restarts within five seconds of a crash.

Why This Matters

In production Linux environments, systemd eliminates the 30MB memory overhead associated with PM2 while providing built-in logging via journald. This native approach reduces the infrastructure attack surface and dependency chain by removing the need for npm-installed process managers.

Key Insights

  • Zero-overhead process management compared to PM2’s 30MB memory footprint (TateLyman, 2026)
  • Automated recovery cycles via Restart=always and RestartSec=5 parameters for crashed services
  • Native journald integration handles log rotation and historical data without manual configuration
  • Support for multi-user.target ensures application persistence across system reboots
  • Production-ready reliability proven by a 4,500-line trading bot operating for months on Oracle Cloud

Working Examples

systemd service unit file configuration for Node.js

[Unit]\nDescription=My Node.js App\nAfter=network.target\n[Service]\nType=simple\nUser=ubuntu\nWorkingDirectory=/home/ubuntu/my-app\nExecStart=/usr/bin/node app.js\nRestart=always\nRestartSec=5\nEnvironment=NODE_ENV=production\nStandardOutput=journal\nStandardError=journal\n[Install]\nWantedBy=multi-user.target

Native logging commands using journalctl

journalctl -u myapp -f # Live logs\njournalctl -u myapp --since today # Today's logs\njournalctl -u myapp -n 100 # Last 100 lines

Core service management commands

sudo systemctl start myapp # Start\nsudo systemctl stop myapp # Stop\nsudo systemctl restart myapp # Restart\nsudo systemctl status myapp # Check status\nsudo systemctl enable myapp # Start on boot

Practical Applications

  • Use Case: Deploying resource-intensive applications on low-memory VMs like Oracle Cloud Free Tier. Pitfall: Using PM2 on limited instances can lead to memory exhaustion due to its 30MB overhead.
  • Use Case: Standardizing logging across microservices using journald for live monitoring. Pitfall: Implementing custom log rotation logic that conflicts with built-in system management.
  • Use Case: Ensuring critical services survive hardware reboots using systemctl enable. Pitfall: Manual process management fails to restore services after infrastructure maintenance.

References:

Continue reading

Next article

Optimizing AI-Generated Testing: High-Stakes Strategies for Claude Code

Related Content