Skip to content

Instantly share code, notes, and snippets.

@andreasonny83
Created July 20, 2019 13:07
Show Gist options
  • Save andreasonny83/c35b51c4197d09af1b8c0510c0b1d1ea to your computer and use it in GitHub Desktop.
Save andreasonny83/c35b51c4197d09af1b8c0510c0b1d1ea to your computer and use it in GitHub Desktop.
Run node app in background

Run node app in background

Running a script in the background in linux can be done using nohup, using nohup we can run node application in the background

$ nohup node server.js > /dev/null 2>&1 &
  • nohup means: Do not terminate this process even when the stty is cut off
  • > /dev/null means: stdout goes to /dev/null (which is a dummy device that does not record any output)
  • 2>&1 means: stderr also goes to the stdout (which is already redirected to /dev/null) You may replace &1 with a file path to keep a log of errors, e.g.: 2>/tmp/myLog
  • & at the end means: run this command as a background task

Forever

Forever is another solution for Node scripts

Installation

$ npm install forever -g

Usage

$ forever start /nodeapp/index.js
$ forever restart /nodeapp/index.js
$ forever stop /nodeapp/index.js
$ forever list

Kill Process

You can stop the process using the kill command as well:

First you need to know which process ID to kill, list all the process running node by running:

ps axl | grep node

The second column of your result is probably the PID, take that number and run the command below:

kill -9 [PID]
@andreasonny83
Copy link
Author

andreasonny83 commented Mar 31, 2020

Thank you for sharing 👍

Nice, happy to help!
That was an old note by the way. I would suggest using something like Concurrently now. Have a look at https://github.com/kimmobrunfeldt/concurrently#readme

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