Introduction
forever is a Node.js CLI tool and library that keeps your scripts running continuously. If a process crashes or exits unexpectedly, forever automatically restarts it, making it a straightforward solution for keeping Node.js services alive in production or development environments without complex orchestration.
What forever Does
- Starts Node.js scripts as background daemon processes
- Automatically restarts scripts when they crash or exit
- Provides listing, stopping, and log management for running processes
- Supports custom environment variables and command-line arguments per process
- Can run as both a CLI tool and a programmatic Node.js module
Architecture Overview
forever uses a monitor/worker pattern. The CLI spawns a monitor process that watches the child script. When the child exits, the monitor evaluates the exit code and restart policy, then re-spawns if appropriate. Each managed process gets its own log files for stdout and stderr. The monitor communicates over IPC to the CLI for commands like list, stop, and restart. A shared socket file tracks all active forever processes on the system.
Self-Hosting & Configuration
- Install globally:
npm install -g forever - Start a script:
forever start --minUptime 1000 --spinSleepTime 1000 app.js - Set custom log output:
forever start -o out.log -e err.log app.js - Use a JSON config file for multiple processes:
forever start forever.json - Set the
--uidflag to give processes a unique identifier for management
Key Features
- Zero-downtime restarts with configurable minimum uptime thresholds
- Per-process log file management with rotation support
- JSON-based configuration for managing multiple processes
- Programmatic API for embedding in custom deployment scripts
- Spin-sleep delay to prevent rapid restart loops on persistent failures
Comparison with Similar Tools
- PM2 — more feature-rich with clustering, metrics, and a dashboard; forever is simpler and lighter
- nodemon — restarts on file changes during development; forever restarts on crashes in production
- systemd — OS-level service manager; forever is Node.js-specific and easier to set up for Node apps
- Docker restart policies — container-level restarts; forever works inside or outside containers
FAQ
Q: How do I see all running forever processes?
A: Run forever list to see process IDs, uptimes, and log file locations.
Q: Can I limit how many times a script restarts?
A: Yes. Use --max to set a maximum restart count: forever start --max 5 app.js.
Q: Does forever support clustering? A: No. For clustering, consider PM2 or use the Node.js cluster module directly. forever focuses on single-process monitoring.
Q: Can I use forever with non-Node scripts?
A: Yes. Use forever start -c python my_script.py with the -c flag to specify the command interpreter.