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:
It allows to reconnect to the process if it is interactive
Linux - Solution 2:
- 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:
Run anything you want:
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
The favor of PM2 is that it can generate the system startup script to make the process persist between restarts:
Where platform can be ubuntu|centos|redhat|gentoo|systemd|darwin|amazon.forever.js:
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:
Then build your image and run your container: