How to run nodejs script as a deamon

We can use forever utility to run the nodejs script in background. This will ensure the script runs continuously.

Forever can be installed using npm.

#npm install forever -g

Please note that this module is installed globally here.

Now we can start the nodejs process as daemon.

#forever start script.js

Here we need to replace script.js with the correspinding script name.

Instead of running the script directly, we can create one json file with more options.

Lets see one example of app.json.

{
    "uid": "app",
    "append": true,
    "watch": true,
    "script": "index.js",
    "sourceDir": "/app",
    "logFile": "/app/logs/forever.log",
    "outFile": "/app/logs/out.log",
    "errFile": "/app/logs/error.log"
}

In this method, we can start the application by calling the json file.

#forever start app.json

The different options for forever is avaialbe in the help option.

#forever --help

That's all…

Leave a Reply

Your email address will not be published. Required fields are marked *