Nodejs is generally used as a user command execution, when the user disconnected customers, the use of the stop, very annoying. How to make Nodejs application as a service and execute it in the background?
The simplest way:
$ nohup node app.js &
However, forever can do more things, such as recording output and error logs separately, such as can be used as an API in JS.
$ sudo npm install forever -g #安装$ forever start app.js #启动$ forever stop app.js #关闭$ forever start -l forever.log -o out.log -e err.log app.js #输出日志和错误
command syntax and use of Https://github.com/nodejitsu/forever
A few benefits of personal forever
1, daemon do not write their own 2, automatic restart, especially for Web project 3, process management
Forever-w app.js-w parameter is automatic monitoring file changes, file modification saved automatic restart App.js. Very cool. The only thing that bothers me is that. Foreverignore does not work, the author has not changed.
Essentially, under the forever process, create a child process for the node app.
Forever Instructions for use
1. Simple Start-upForeverStartApp.Js2. Specifies the forever information output file, which, of course, is put to ~/.forever/forever.log by defaultForeverStart-LForever.LogApp.Js3. Specifies the log information and error log output files in the app.js.-O is the Console.log output information,-e is the Console.error output informationForeverStart-o out. Log -e err. Log app. Js//4. Append log, forever default is not overwrite the last boot log, //so if the second boot does not add-a, it will not let run start -l forever.log -a app. Js//5. Listen for all file changes under the current folder forever start -w app. JS
forever list
// 1. 监听当前文件夹下的所有文件改动(不太建议这样) forever start -w app.js
// 1. 停止所有运行的node App forever stopall // 2. 停止其中一个node App forever stop app.js // 当然还可以这样 // forever list 找到对应的id,然后: forever stop [id]
The restart operation is consistent with the stop operation.
// 1. 启动所有forever restartall
Development and on-line recommended configuration
//development environment node_env= Development forever start -l Forever. Log -e err. Log -a app. Js//online environment node_env=production forever start -l ~ /.forever/forever.log-e ~/.forever/err.log-w-a app.js
Add node_env to let app.js identify what environment is currently used. Without it, you might not know?
This time you need to be aware of configuring environment variables.
SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
We're going to let forever run automatically, first in
/etc/init.d
Directory to create a file
node
, the contents are as follows:
Stop)Forever Stop--Pidfile $PID $DEAMON;;StopAll)Forever StopAll--Pidfile $PID;;Restartall)Forever Restartall--Pidfile $PID;;Reload|Restart)Forever Restart-L $LOG/Forever.Log-o $LOG/Forever_out.log -e $LOG /forever_ Err. Log --pidfile $PID -a $DEAMON ; List Forever list ; *) echo "Usage:/etc.init.d/node {start| Stop|restart|reload|stopall|restartall|list} " exit 1 ;; esacexit0
The above code is my local virtual machine configuration, according to the actual situation to modify the relevant parameters, mainly Deamon path parameters, give the file executable permissions, and run Chkconfig add Autorun:
reboot重启系统,通过浏览器进入网站可发现,该NodeJS已经可自动运行了……
Forever let Nodejs apply background execution