pm2-recover
v1.0.5
Published
A utility to recreate pm2 start commands from a PM2 dump file
Maintainers
Readme
PM2 Dev Mode Recovery Utility
pm2-recover reconstructs your pm2 start commands after nvm switches or OS reinstalls by stripping absolute paths.
Unlike pm2 resurrect or pm2 update, pm2-recover deliberately strips pm2's saved absolute binary paths and environment details, maintaining only the directory from which the process was started from and the pm2 start args.
Usage:
# Save to file for review / editing (recommended)
npx pm2-recover -o recover.sh
bash recover.sh
# One-liner (if you're feeling lucky)
npx pm2-recover | bashOptions:
-f,--dumpFile<path>: (Optional) Specify a custom PM2 dump file path. Defaults to~/.pm2/dump.pm2.-o,--outFile<path>: (Optional) Write generated commands to a file instead of stdout.-h,--help: (Optional) Prints a help message.
Features:
- Preserves
pm2 --watchmode - Preserves original working directories
- Preserves stopped status (
pm2 start→pm2 stopimmediately) - Fixes nvm absolute paths
- Leaves your exact command untouched when using
bash -c '...' - Works with
pm2managed other-languages (e.g.go run .,bun start:prod --watch,npm run dev)
Important:
- Run
pm2 savebefore switching Node versions or running this tool to preserve your active process list. pm2-recoverdoes not modify your dump, PM2 system files, or existing processes. It only reads your dump and generates shell commands.pm2-recoverdoes not reconstruct environment variables (yourprocess.env).- This tool doesn't install
pm2/nvm/nodefor you.
Example:
npx pm2-recover -f /path/to/custom-pm2-dump.json: custom dump.pm2 path.npx pm2-recover -o recovery-script.sh: writes the output to a file.npx pm2-recover | bash: directly try to execute the output by piping to bash.
Here's an anonymized actual output from npx pm2-recover straight from my own dev machine:
#!/bin/bash
# --- PM2 Relaunch Commands Generated by pm2-recover ---
# Input File: /home/developer/.pm2/dump.pm2
# Structure: pm2 start --name NAME COMMAND [OPTIONS] -- ARGS
# Sets bash to stop script execution immediately after error
set -euo pipefail
# Execute pm2 kill first to ensure all pm2 processes are freshly recreated.
# Skip this line if you don't need to.
pm2 kill || true
# [Shell-Wrapped] Process: web-client
cd "/path/to/project/web-client" && pm2 start --name web-client 'npm run dev'
# [Shell-Wrapped] Process: api-gateway
cd "/path/to/project/api-gateway" && pm2 start --name api-gateway 'gow run .'
# [Shell-Wrapped] Process: data-processor
cd "/path/to/project/data-processor" && pm2 start --name data-processor 'bun consume:events'
pm2 stop data-processor
# [Shell-Wrapped] Process: analytics-backend
cd "/path/to/project/analytics-backend" && pm2 start --name analytics-backend 'npm run start:dev'
# [Shell-Wrapped] Process: content-editor
cd "/path/to/project/content-editor" && pm2 start --name content-editor 'npx parcel public/*.html --port 1234'
pm2 stop content-editor
# [Direct Exec] Process: auth-service
cd "/path/to/project/authService/entrypoints" && pm2 start --name auth-service './auth-middleware' -- '--config' 'original.yaml'
# --- FINALIZATION ---
pm2 save
pm2 listWhen to use
pm2-recover might help you if:
- pm2 resurrect fails after switching Node versions via nvm
- PM2 shows your app as errored due to missing binary paths
- Your PM2 dump contains values like
/home/user/.nvm/versions/node/vXX/bin - You reinstalled your OS or moved your workspace
- You want portable, clean pm2 start commands
- You have a lot of pm2 processes you need to evaluate how it starts
Why pm2 resurrect Fails After nvm Node Version Switches
For a dev machine, pm2 is great to keep a lot of services running and to start/stop services as we switch projects without having to remember the start script.
However, when we run pm2 without an ecosystem.config.js, we might run a service like this:
cd /home/developer/projects/myDevApp/
pm2 start --name myDevApp 'npm run dev'Which can result in something like this within dump.pm2:
{
"args": [
"-c",
"npm run dev"
],
"name": "myDevApp",
"pm_cwd": "/home/developer/projects/myDevApp",
"pm_exec_path": "/usr/bin/bash",
"status": "online",
"watch": false,
// 👇 NVM_DIR 👇 versioned NodeJS
"PATH": "/home/developer/.nvm/versions/node/v24.11.1/bin:/usr/local/bin:/usr/bin:/usr/local/sbin"
// versioned npm/node binary is here 👆🏻
}Which is great for servers where we might have consistent paths. The moment we:
- changed node version and have
nvmto remove the older version - change
NVM_DIR
Or other things that makes the old execution environment no longer available, pm2 resurrect breaks.
The easiest way to really "restore" the wounded service is cd to the directory and re-run pm2 start -- exactly what pm2-recover tries to help you with.
