Run Your Node.js Scripts Continuously with Forever
What is Forever?
Forever is an npm module that ensures your Node.js script runs continuously in the background on your server. This helpful CLI tool is perfect for production environments, making it easy to manage Node applications and their processes.
Installing Forever
To get started, you’ll need to install Forever globally using the following command:
npm install -g forever
Running a Script with Forever
Running a script with Forever is straightforward. Simply use the following command, replacing “app.js” with the name of your script:
forever start app.js
You can also add a -a
flag to append log files to the default log file location. Additionally, you can run multiple scripts at once by separating script names with spaces.
Managing Processes with Forever
Forever allows you to list all running processes using the list
command. This provides valuable information, including process ID, script name and location, log file location, uptime duration, and more.
To stop a running script, use the stop
command followed by the index ID, script name, UID, or process ID. You can also assign a unique name when starting a process using the --uid
flag.
Configuring and Executing a JSON File with Forever
Forever also allows you to configure a JSON file with all the necessary details and execute it. This file can define multiple processes, making it easy to manage complex applications.
Using Forever Without the CLI
The forever-monitor
module enables you to run processes programmatically without using the CLI. This is particularly useful when you need to integrate Forever into your application code.
To use forever-monitor
, install the dependency using npm and create a separate file to define the options. Then, import the module and set up the options to run your app.
Getting Started with Forever
Forever is a simple yet powerful tool that enables developers to run Node scripts continuously. While it may lack some features, it’s definitely worth trying out to see if it suits your use case.
Happy Hacking!