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 field | systemd directive | Notes |
|---|---|---|
| name | unit filename | myapp.service or [email protected] |
| script + interpreter | ExecStart= | Use absolute paths (e.g. /usr/bin/node) |
| cwd | WorkingDirectory= | Required for relative paths in your code |
| user | User= | Drop privileges; avoid running as root |
| env | Environment= | One directive per variable, or use EnvironmentFile= |
| instances > 1 / max | template unit @ | [email protected], [email protected], ... |
| autorestart: true | Restart=on-failure | Or Restart=always to mirror PM2 default |
| max_memory_restart | MemoryMax= | Kills service if memory exceeded; restart handles relaunch |
| pm2 logs | journalctl -u name | Logs 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.jswill fail. Use/usr/bin/node /var/www/app/server.js. Runwhich nodeto find the absolute path. - .env files are not auto-loaded. PM2 reads
ecosystem.config.js; systemd does not load.env. UseEnvironmentFile=/etc/myapp.envor list each variable withEnvironment=KEY=value. - Run as a non-root user. Set
User=andGroup=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-reloadbeforesystemctl restart, otherwise systemd uses the cached version. - Logs live in journald. Use
journalctl -u myapp -fto tail logs instead ofpm2 logs. AddSyslogIdentifier=to make filtering easier.
Frequently Asked Questions
What is the difference between PM2 and systemd?
Can I use PM2 with systemd?
Where do systemd service files go?
What does Restart=on-failure do in systemd?
What is the difference between PM2 cluster mode and fork mode?
Related Tools
Need to manage SSH connections?
SSH Workbench lets you connect, browse files, and manage servers visually.
Try SSH Workbench Free