Skip to content

Instantly share code, notes, and snippets.

@milose
Last active January 17, 2023 06:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milose/399e8ff3e7d03f658dfe to your computer and use it in GitHub Desktop.
Save milose/399e8ff3e7d03f658dfe to your computer and use it in GitHub Desktop.
Making your node apps run on startup and forever

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

Create a forever.json for your apps

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.

Auto starting on boot with cron

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

Taking control

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

Conclusion

Now your everytime you reboot, all apps in forever.json will run in background and be rerun if they fail.

Neat.

published: true
@andyp17
Copy link

andyp17 commented Feb 11, 2019

Hello,
I know, this article was created a long time ago. But maybe I have a chance to get an answer. I followed your instructions and they worked well. But I have one issue: I have also a python script which I have to run with forever. To do so, I start the script with "forever start -c python3.6 get_temp.py". How shall I put this line into the forever.json to get the script automatically run at startup? Many thanks!!
Kr
Andy

@SpeedrunnerG55
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment