On a computer to manually run node project simple, node xx.js, want to let node project background execution, although not directly with node command, but after installing forever this package. It's still very relaxing. Just build the node project on the remote server. Suppose it's not self-priming. Once the server restarts, the project must be manually enabled by the administrator remotely enough to execute.
Is it possible to solve these problems with forever and startup scripting? The answer, of course, is yes. Just a little bit of trouble. and forever official lack of specific configuration documents. I also took some detours in the configuration, the following specific.
Note: The experimental environment in this article is Ubuntu Server 12.04 LTS x86_64, which is simpler to configure on CentOS
First, I want to try to add a sentence in/etc/rc.local forever start xxx Look, the results found that Ubuntu (other systems like) do not bird me, the main contradiction is mongodb use such a way to run up, forever can not. Helpless under, or from the angle of/ETC/INIT.D to consider it.
First of all thanks to this article and its author, Http://cnodejs.org/topic/5059ce39fd37ea6b2f07e1a3, is really a few precious materials!
The premise is to first forever good. The method is very easy to run, such as the following command:
NPM Install Forever-g
Once the installation is complete, test it with a simple node program:
Forever Start Test.jsforever stop test.jsforever restart Test.js
Just do not prompt error, it indicates that forever is able to use, that is to use forever to open a node project in the background of the basic conditions already have, the rest is to write a startup script.
The basic contents of the script are as follows. Thanks to the original author for his hard work:
#!/bin/bash### BEGIN INIT info# provides: xiyoulib# Required-start: $all # R Equired-stop: $all # Default-start: 2 3 4 * default-stop: 0 1 6# short-des Cription:start daemon at boot time# Description: Enable service provided by daemon.### END INIT INFO # chkconfig:345 08# description:forever for node.jsdeamon=/node.js/xiyoulibnodeexpress/bin/www #这里须要填写你自己的Node项目的启 Script file Log=/node.js/log/log #可选. The log file folder Pid=/node.js/pid #必填内容. Process number used to record forever export path= $PATH:/usr/local/bin #在这里指定一下Node的可运行程序安装文件夹, mine is/usr/local/binexport node_path= $NODE _ Path:/usr/local/lib/node_modules #这里是Node类库的路径 # The content will not be changed Node=nodeforever=forevercase "in Start" $forever Start-l $LOG--pidfile $PID-a $DEAMON; Stop) $forever stop--pidfile $PID $DEAMON;; StopAll) $forever stopall--pidfile $PID;; Restartall) $forever Restartall--pidfile $PID;; Reload|restart) $forever restart-l $LOG--pidfile $PID-a $DEAMON; list) $forever list;; *) echo "Usage:/etc.init.d/node {start|stop|restart|reload|stopall|restartall|list}" exit 1;; Esac
Here's a reminder: it's a good idea to create a separate folder under the root folder for the node project, such as/node.js. Then the permission is set to 754, which can avoid some permissions problems caused by the trouble.
Because the Ubuntu server system is used. The above also configures the MongoDB startup service, and adds the following statement to its INIT.D script:
# Required-start: $all # required-stop: $all
So in the future when you join the system will prompt the error, so in the Start Script node Project I added a previous string of description gaze, so that the Ubuntu server system settings, assuming on the CentOS. There should be no such problem, pay special attention to this point!
That is, the following description information:
# # # BEGIN INIT info# provides:xiyoulib# required-start: $all # required-stop: $all # default-start:2 3 4 AA default-stop:0 1 6# short-description:start daemon at boot time# description:enable service provided B Y daemon.### END INIT INFO
After the script has been edited, use the Chkconfig--list directive to see if the service you are joining takes effect, that is, 3 and 5 all are capable of booting from on.
Assuming that 3 and 5 are not set to ON, then run Chkconfig--level 35 [your service name] on is OK. Ubuntu server may report some warning. But just to be able to turn on 3 and 5 of the services you need to set, other errors can be ignored (I feel this is the system's own business).
After setting up the node project can be implemented on the Linux self-start, can shutdown-r now test whether it can be self-initiated, after the start of the right to visit you set the port number, virtual folder God horse, assuming that the desired is done.
But if the hypothesis is not correct, check some scripts carefully. Then according to the error related changes, after all, I also tried out.
Original address: http://blog.csdn.net/yuanguozhengjust/article/details/37512993, reprint please indicate the source!
Using forever to implement node. JS Project Self-start on Linux