Configs2026年7月7日·1 分钟阅读

forever — Keep Node.js Scripts Running Continuously

A CLI tool that ensures a given Node.js script runs continuously, restarting it on failure.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
forever Overview
直接安装命令
npx -y tokrepo@latest install 118e56f4-799e-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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 --uid flag 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.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产