PM2 to systemd Converter

Generate a systemd .service unit file from a PM2 app config.

Client-side only — nothing leaves your browser

PM2 App Configuration

systemd unit: /etc/systemd/system/myapp.service

[Unit]
Description=myapp - Node.js application
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/node /var/www/myapp/server.js
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=myapp

[Install]
WantedBy=multi-user.target

Install commands

sudo nano /etc/systemd/system/myapp.service
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service
sudo systemctl status myapp.service

Converting PM2 to a systemd Service

PM2 is a process manager for Node.js that handles startup, restarts, logs, and clustering. systemd is the native init and service manager on most modern Linux distributions (Ubuntu, Debian, RHEL, CentOS, Fedora). Replacing PM2 with a systemd unit file removes a runtime dependency, reduces memory footprint, and integrates your Node app directly into the host's service supervision, logging (journald), and boot ordering. This converter maps the most common PM2 fields — name, script, cwd,user, env, instances, and max_memory_restart — to their systemd equivalents.

PM2 to systemd Field Mapping

PM2 fieldsystemd directiveNotes
nameunit filenamemyapp.service or [email protected]
script + interpreterExecStart=Use absolute paths (e.g. /usr/bin/node)
cwdWorkingDirectory=Required for relative paths in your code
userUser=Drop privileges; avoid running as root
envEnvironment=One directive per variable, or use EnvironmentFile=
instances > 1 / maxtemplate unit @[email protected], [email protected], ...
autorestart: trueRestart=on-failureOr Restart=always to mirror PM2 default
max_memory_restartMemoryMax=Kills service if memory exceeded; restart handles relaunch
pm2 logsjournalctl -u nameLogs go to journald by default

Cluster Mode and Template Units

PM2's cluster mode forks N copies of your app and load-balances connections across them via the Node.js cluster module. systemd has no built-in load balancer, but the standard pattern is a template unit file named with an @ suffix (e.g. [email protected]). Each instance is enabled by appending an instance identifier — systemctl enable myapp@1, myapp@2, etc. The%i specifier inside the unit expands to that identifier, which you can read in your app via the INSTANCE_ID env var to offset ports or pick a worker id. For real load balancing, put nginx, HAProxy, or a Linux SO_REUSEPORT-capable listener in front of the instances.

Common Gotchas When Migrating

  • Use absolute paths in ExecStart. systemd does not consult $PATH, so node server.js will fail. Use /usr/bin/node /var/www/app/server.js. Run which node to find the absolute path.
  • .env files are not auto-loaded. PM2 readsecosystem.config.js; systemd does not load .env. Use EnvironmentFile=/etc/myapp.env or list each variable with Environment=KEY=value.
  • Run as a non-root user. Set User= and Group= to avoid running the Node process as root. Make sure that user owns the working directory and log files.
  • Reload after edits. After changing the unit file, run sudo systemctl daemon-reload before systemctl restart, otherwise systemd uses the cached version.
  • Logs live in journald. Use journalctl -u myapp -f to tail logs instead of pm2 logs. Add SyslogIdentifier= to make filtering easier.

Frequently Asked Questions

What is the difference between PM2 and systemd?
PM2 is a userland Node.js process manager that handles startup, clustering, log rotation, and zero-downtime reloads. systemd is the system-level init and service manager built into most Linux distributions (Ubuntu, Debian, RHEL, Fedora) — it manages every service on the machine, not just Node apps. PM2 is feature-rich for Node-specific workflows; systemd is universal, has no runtime dependency, integrates with journald for logs, and starts before any user session. For production, many teams drop PM2 and use systemd directly to reduce moving parts.
Can I use PM2 with systemd?
Yes — this is what the 'pm2 startup systemd' command does. PM2 generates a systemd unit (typically named pm2-<user>.service) that starts the PM2 daemon at boot, which in turn launches your saved app list (via 'pm2 save'). This gives you PM2's CLI on top of systemd's boot supervision. The trade-off is two layers of process management: systemd watches PM2, and PM2 watches your apps. Converting directly to native systemd units (what this tool generates) eliminates that extra layer.
Where do systemd service files go?
Custom unit files belong in /etc/systemd/system/. Place your generated .service file there, then run 'sudo systemctl daemon-reload' so systemd picks it up. Distribution packages install their units in /usr/lib/systemd/system/ (or /lib/systemd/system/ on Debian-based systems) — do not edit those, as package upgrades will overwrite them. User-level units (no sudo, run only when the user is logged in) go in ~/.config/systemd/user/.
What does Restart=on-failure do in systemd?
Restart=on-failure tells systemd to restart the service automatically if it exits with a non-zero status code, is killed by a signal, times out, or hits a watchdog timeout — but NOT if it exits cleanly (exit code 0) or is stopped manually with systemctl stop. This mirrors PM2's autorestart behavior for crashes. If you want PM2's exact default (restart on every exit, including clean ones), use Restart=always instead. Pair it with RestartSec=5 to throttle rapid restart loops.
What is the difference between PM2 cluster mode and fork mode?
In fork mode (the PM2 default), PM2 spawns a single child process running your script. In cluster mode, PM2 uses Node's cluster module to fork N workers that share the same listening port via the OS, giving you load balancing across CPU cores without a reverse proxy. systemd has no equivalent built-in cluster mode — the standard pattern is a template unit ([email protected]) with one systemd-managed instance per worker, fronted by nginx/HAProxy or relying on SO_REUSEPORT. This converter generates a template unit automatically when instances > 1.

Related Tools

Need to manage SSH connections?

SSH Workbench lets you connect, browse files, and manage servers visually.

Try SSH Workbench Free