Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

forever

> 编程语言
Open source

A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)

13.8K stars0 likes4 views
WebsiteGitHub

About

A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)

forever

A simple CLI tool for ensuring that a given script runs continuously (i.e. forever). Note that this project currently fully depends on the community for implementing fixes and new features. For new installations we encourage you to use pm2 or nodemon

Installation

bash
  $ [sudo] npm install forever -g

Note: If you are using forever programmatically you should install forever-monitor.

bash
  $ cd /path/to/your/project
  $ [sudo] npm install forever-monitor

Usage

There are two ways to use forever: through the command line or by using forever in your code. Note: If you are using forever programatically you should install forever-monitor.

Command Line Usage

You can use forever to run scripts continuously (whether it is written in node.js or not).

Example

forever start app.js

Options

…

There are several examples designed to test the fault tolerance of forever. Here's a simple usage example:

bash
  $ forever -m 5 examples/error-on-timer.js

JSON Configuration Files

In addition to passing forever the path to a script (along with accompanying options, described above), you may also pass forever the path to a JSON file containing these options. For example, consider an application with the following file structure:

.
├── forever
│   └── development.json
└── index.js

// forever/development.json
{
	// Comments are supported
    "uid": "app",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/home/myuser/app",
    "logFile": "/home/myuser/logs/forever.log",
    "outFile": "/home/myuser/logs/out.log",
    "errFile": "/home/myuser/logs/error.log"
}

This application could be started with forever, as shown below:

bash
$ forever start ./forever/development.json

Absolute paths to such configuration files are also supported:

bash
$ forever start /home/myuser/app/forever/development.json

Note: Forever parses JSON configuration files using shush, allowing the use of in-line comments within such files.

Multi-App Configuration Files

JSON configuration files can also be used to define the startup options for multiple applications, as shown below.

[
  {
    // App1
    "uid": "app1",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/home/myuser/app1"
  },
  {
    // App2
    "uid": "app2",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/home/myuser/app2",
    "args": ["--port", "8081"]
  }
]

Using In Your Code

The forever module exposes some useful methods to use in your code. Each method returns an instance of an EventEmitter which emits when complete. See the forever cli commands for sample usage.

Remark: As of [email protected] processes will not automatically be available in forever.list(). In order to get your processes into forever.list() or forever list you must instantiate the forever socket server:

javascript
  forever.startServer(child);

This method takes multiple forever.Monitor instances which are defined in the forever-monitor dependency.

forever.load (config)

Synchronously sets the specified configuration (config) for the forever module. There are two important options:

Option Description Default
root Directory to put all default forever log files forever.root
pidPath Directory to put all forever *.pid files [root]/pids
sockPath Directory for sockets for IPC between workers [root]/sock
loglength Number of logs to return in forever tail 100
columns Array of columns to display when format is true forever.config.get('columns')
debug Boolean value indicating to run in debug mode false
stream Boolean value indicating if logs will be streamed false

forever.start (file, options)

Starts a script with forever. The options object is what is expected by the Monitor of forever-monitor.

forever.startDaemon (file, options)

Starts a script with forever as a daemon. WARNING: Will daemonize the current process. The options object is what is expected by the Monitor of forever-monitor.

forever.stop (index)

Stops the forever daemon script at the specified index. These indices are the same as those returned by forever.list(). This method returns an EventEmitter that raises the 'stop' event when complete.

forever.stopAll (format)

Stops all forever scripts currently running. This method returns an EventEmitter that raises the 'stopAll' event when complete.

The format parameter is a boolean value indicating whether the returned values should be formatted according to the configured columns which can set with forever columns or programmatically forever.config.set('columns').

forever.list (format, callback)

Returns a list of metadata objects about each process that is being run using forever. This method will return the list of metadata as such. Only processes which have invoked forever.startServer() will be available from forever.list()

The format parameter is a boolean value indicating whether the returned values should be formatted according to the configured columns which can set with forever columns or programmatically forever.config.set('columns').

forever.tail (target, options, callback)

Responds with the logs from the target script(s) from tail. There are two options:

  • length (numeric): is is used as the -n parameter to tail.
  • stream (boolean): is is used as the -f parameter to tail.

forever.cleanUp ()

Cleans up any extraneous forever *.pid files that are on the target system. This method returns an EventEmitter that raises the 'cleanUp' event when complete.

forever.cleanLogsSync (processes)

Removes all log files from the root forever directory that do not belong to current running forever processes. Processes are the value returned from Monitor.data in forever-monitor.

forever.startServer (monitor0, monitor1, ..., monitorN)

Starts the forever HTTP server for communication with the forever CLI. NOTE: This will change your process.title. This method takes multiple forever.Monitor instances which are defined in the forever-monitor dependency.

Logging and output file locations

By default forever places all of the files it needs into /$HOME/.forever. If you would like to change that location just set the FOREVER_ROOT environment variable when you are running forever:

FOREVER_ROOT=/etc/forever forever start index.js

Make sure that the user running the process has the appropriate privileges to read & write to this directory.

Run Tests

bash
  $ npm test

License: MIT

Author: Charlie Robbins

Maintainer: Igor Savin

Contributors: Fedor Indutny, James Halliday, Charlie McConnell, Maciej Malecki, John Lancaster

Issues· 343 open

View all issuesOpen on GitHub
  • #1077

    [node v14.x+] Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency

    Updated Nov 9, 2025
  • #1149

    Security Vulnerability Warning: minimist Version 0.0.10 [CVE-2021-44906]

    Updated Jan 31, 2025
  • #1145

    Log Bigsize

    Updated Nov 6, 2024
  • #1146

    Improve documentation of `--killSignal`

    Updated Sep 25, 2024
  • #1143

    redhat 9 enterprise clean install, getting Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency

    Updated Sep 19, 2024
  • #904

    forever list, and other commands, hang after starting with --spinSleepTime 30000 --minUptime 10000

    Updated Jul 11, 2024
  • #1144

    uptime format

    Updated Apr 27, 2024
  • #1142

    forever --version Can run, but there are errors(v4.0.3)

    Updated Mar 11, 2024
  • #1026

    process is still running though forever list is empty

    Updated Dec 4, 2023
  • #1141

    how to save old history log instead of delete..

    Updated Sep 21, 2023

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言