LinuxCommandLibrary

forever

Run Node.js scripts continuously in the background

TLDR

Start running a file forever (as a daemon)

$ forever [script]
copy

List running "forever" processes (along with IDs and other details of "forever" processes)
$ forever list
copy

Stop a running "forever" process
$ forever stop [ID|pid|script]
copy

SYNOPSIS

forever [options] [script-args]
forever [options]

PARAMETERS

start
    Starts the script and daemonizes it, keeping it running in the background.

stop
    Stops the forever process identified by its unique ID (UID) or script path.

stopall
    Stops all forever processes currently running.

restart
    Restarts a specific forever process by its UID or script path.

restartall
    Restarts all forever processes.

list
    Lists all forever processes that are currently running, showing their status and UIDs.

logs
    Displays the logs (stdout/stderr) for a specific forever process.

cleanlogs
    Cleans up old forever log files.

-s, --silent
    Suppresses output messages from forever itself, showing only errors from the managed script.

-o, --outFile
    Path to a file for capturing the standard output (stdout) of the script.

-e, --errFile
    Path to a file for capturing the standard error (stderr) of the script.

-l, --logFile
    Path to the forever log file, which records forever's internal messages and actions.

-p, --pidFile
    Path to a file for storing the forever process's Process ID (PID).

-c, --command
    The command used to execute the script (default: node for .js files).

-a, --append
    Appends logs to the specified outFile, errFile, and logFile instead of overwriting them.

-w, --watch [dir]
    Watches for file changes in the script's directory (or specified directory) and automatically restarts the script upon modification.

--minUptime
    Minimum uptime in milliseconds before a script is considered stable (default: 0).

--spinSleepTime
    Time in milliseconds to wait before restarting a script after it crashes (default: 5000ms).

--killSignal
    The signal to send to the process to terminate it (e.g., SIGTERM, SIGKILL).

--uid
    Assigns a unique ID to the forever process, making it easier to manage and identify (e.g., my-app).

--sourceDir


    Absolute path to the script's source directory, used when forever changes directory.

--workingDir
    Absolute path to the script's working directory, where the script will be executed.

--max
    The maximum number of times forever will attempt to restart the script before giving up.

--plain
    Disables colors from forever output, useful for plain text logging.

DESCRIPTION

forever is a simple command-line interface (CLI) tool for Node.js applications designed to ensure that a given script runs continuously. It daemonizes the specified Node.js script, keeping it alive even if it crashes or exits unexpectedly. When a script monitored by forever terminates, forever automatically restarts it, providing basic process uptime management. It supports redirecting standard output and error to log files, watching for file changes to trigger restarts, and managing multiple Node.js processes concurrently. While not as feature-rich as more advanced process managers like PM2 or systemd, forever remains a popular choice for straightforward deployments and background script execution due to its simplicity and ease of use.

CAVEATS

While forever is excellent for simple Node.js process management, it has limitations. It lacks advanced features found in PM2, such as built-in clustering, load balancing, or detailed monitoring dashboards. It doesn't integrate directly with system-level init systems like systemd without additional configuration. For complex production environments or applications requiring high availability and sophisticated scaling, more robust solutions might be preferred. Furthermore, if the forever process itself crashes, it will cease to monitor and restart any daemonized scripts.

INSTALLATION

forever is typically installed via npm (Node Package Manager). To install it globally on your system, use the command:
npm install -g forever

USAGE EXAMPLES

To start a Node.js script:
forever start app.js

To start a script and redirect its output to specific log files:
forever start -o /var/log/my_app_out.log -e /var/log/my_app_err.log app.js

To list all running forever processes:
forever list

To stop a specific process by its UID (e.g., if '0' is its UID from forever list):
forever stop 0

To watch for changes in the script's directory and restart it automatically:
forever start -w app.js

HISTORY

forever originated as a practical utility for Node.js developers in the early days of Node.js, when deploying and maintaining long-running Node.js processes was a common challenge. It provided a straightforward way to keep scripts running in the background and automatically restart them upon failure. Its development focused on simplicity and ease of use, quickly becoming a popular choice for smaller projects and development environments. Although more feature-rich alternatives like PM2 have emerged, forever remains a relevant tool, especially for those seeking a lightweight and low-overhead solution for continuous script execution.

SEE ALSO

pm2(1), systemd(1), nohup(1), screen(1), tmux(1)

Copied to clipboard