How to use the command 'forever' (with examples)
The ‘forever’ command is a server-side JavaScript application that ensures Node.js applications run indefinitely by automatically restarting them after they exit. This can be useful for running Node.js applications as daemons or in production environments where high availability is required. The ‘forever’ command provides several use cases that allow for managing and monitoring the running Node.js applications.
Use case 1: Start running a file forever (as a daemon)
Code:
forever script
Motivation: By using the ‘forever’ command to start running a file forever, you can ensure that your Node.js application remains running continuously even after it exits. This is particularly useful for long-running processes or applications that need to be restarted automatically in case of failure.
Explanation: The ‘forever script’ command starts running the specified script file, treating it as a daemon that should be kept alive indefinitely. The ‘script’ argument represents the path to the script file that you want to run.
Example output:
info: Forever processing file: script.js
data: index.js started (pid=1234)
In the example output, the ‘forever’ command starts running the ‘script.js’ file and outputs information about the process, including the file name and the process ID (pid).
Use case 2: List running “forever” processes
Code:
forever list
Motivation: Sometimes, you may want to check the status of the “forever” processes that are currently running on your system. The ‘forever list’ command provides a convenient way to view a list of these processes, including their IDs and other details.
Explanation: The ‘forever list’ command lists all the running “forever” processes along with their associated IDs, script names, and other details. It gives you an overview of the applications that are currently being managed by the ‘forever’ command.
Example output:
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] VvXj node /path/to/script1.js 1234 5678.log 5:32:18:52
data: [1] CeXQ node /path/to/script2.js 4321 9876.log 3:12:47:15
In the example output, the ‘forever list’ command displays a list of two running “forever” processes. Each process is represented by a unique ID (e.g., [0], [1]) and includes information about the associated script file, process ID, logfile, and uptime.
Use case 3: Stop a running “forever” process
Code:
forever stop ID|pid|script
Motivation: At times, you may need to stop a running “forever” process to either update the script or terminate it. The ‘forever stop’ command allows you to gracefully stop a specific process managed by “forever”.
Explanation: The ‘forever stop’ command stops a running “forever” process identified by either the process ID (pid), script name, or the ID displayed in the ‘forever list’ output. You can choose any one of these identifiers to stop the target process.
Example output:
info: Forever stopped process:
data: uid command script forever pid logfile uptime
data: [0] VvXj node /path/to/script1.js 1234 5678.log 5:32:18:52
In the example output, the ‘forever stop’ command stops the “forever” process with the ID [0]. The output confirms that the process associated with the script ‘/path/to/script1.js’ has been successfully stopped.
Conclusion:
The ‘forever’ command is a powerful tool for managing Node.js applications, ensuring that they run continuously and are automatically restarted after they exit. By using the ‘forever’ command, you can start running a file forever, list the running “forever” processes, and stop a specific process when necessary. These features help to simplify the process management and monitoring of long-running Node.js applications.