Hello forever
Forever is a simple CLI tool for ensuring that a given script runs continuously (i.e. forever). Install it globally using NPM:
$ [sudo] npm install forever -g
This file should contain settings for all of your apps you want to run at boot. I use /var/www/nodes
to store my node apps and I put my forever.json
to /var/www/nodes/forever.json
.
[
{
"uid": "appName1",
"append": true,
"watch": false,
"script": "index.js",
"sourceDir": "/var/www/nodes/appName1/",
"workingDir": "/var/www/nodes/appName1/"
},
{
"uid": "appName2",
"append": true,
"watch": false,
"script": "index.js",
"sourceDir": "/var/www/nodes/appName2/",
"workingDir": "/var/www/nodes/appName2/"
}
]
You should check the forever documentation for other scenarios.
While there are other methods to run applications after rebooting, I find this one to be universal for most *nix systems.
Open crontab -e
to edit cron job configuration.
Add this line to the bottom of the file:
@reboot /usr/local/bin/forever start /var/www/nodes/forever.json > /dev/null 2>&1
You can now use forever control running tasks.
To list running apps:
forever list
This command lists all running apps with additional information such as uid (defined in forever.json
), uptime, logfile (you can see your app's output here aswell) etc.
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] rds-server /usr/local/bin/node /var/www/nodes/app1/index.js 7776 7786 /root/.forever/app1.log 0:0:2:13.251
To stop a particular task:
forever stop uid
To stop all tasks:
forever stopall
Now your everytime you reboot, all apps in forever.json
will run in background and be rerun if they fail.
Neat.
I see the process running when I do forever list but it seems to be constantly restarting for some reason
error: restarting script because change changed
error: Forever detected script was killed by signal: SIGKILL
error: Script restart attempt #2489