linux - [Solved-5 Solutions] How to run process as background and never die - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to run process as background and never die ?

Linux - Solution 1:

nohup node server.js &
click below button to copy the code. By - Linux tutorial - team

It allows to reconnect to the process if it is interactive

screen
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

nohup node server.js > /dev/null 2>&1 &
click below button to copy the code. By - Linux tutorial - team
  • 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.

Linux - Solution 3:

Start your screen session at first:

user@host:~$ screen
click below button to copy the code. By - Linux tutorial - team

Run anything you want:

wget http://mirror.yandex.ru/centos/4.6/isos/i386/CentOS-4.6-i386-binDVD.iso
click below button to copy the code. By - Linux tutorial - team

Press ctrl+A and then d. Done. Your session keep going on in background.

You can list all sessions by screen -ls, and attach to some by screen -r 20673.pts-0.srv command, where 0673.pts-0.srv is an entry list.

Linux - Solution 4:

node.js process inside a screen session, with the & or even with the nohup flag -- all of them -- are just workarounds.

  • Screen and Tmux are not meant to keep processes running, but for multiplexing terminal sessions.
  • when you are running a script on your server and want to disconnect. But for a node.js server your don't want your process to be attached to a terminal session.
  • To keep things running you need to daemonize the process
# basic usage
$ npm install pm2 -g
$ pm2 start server.js

# you can even define how many processes you want in cluster mode:
$ pm2 start server.js -i 4

# you can start various processes, with complex startup settings
# using an ecosystem.json file (with env variables, custom args, etc):
$ pm2 start ecosystem.json
click below button to copy the code. By - Linux tutorial - team

The favor of PM2 is that it can generate the system startup script to make the process persist between restarts:

$ pm2 startup [platform]
click below button to copy the code. By - Linux tutorial - team

Where platform can be ubuntu|centos|redhat|gentoo|systemd|darwin|amazon.forever.js:

# basic usage
$ npm install forever -g
$ forever start app.js

# you can run from a json configuration as well, for
# more complex environments or multi-apps
$ forever start development.json
click below button to copy the code. By - Linux tutorial - team

Init scripts:

Docker:

Just run your server in a Docker container with -d option and, voilá, you have a daemonized node.js server!

Here is a sample Dockerfile:

FROM node:argon

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]
click below button to copy the code. By - Linux tutorial - team

Then build your image and run your container:

$ docker build -t <your username>/node-web-app .
$ docker run -p 49160:8080 -d <your username>/node-web-app
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

$ nohup node server.js &
[1] 1711
$ disown -h %1
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - How to run process as background and never die