Node Background Management plugin Forever

Source: Internet
Author: User


On a computer to manually run node project simple, node xx.js, want to let node project background run, although not directly with node command, but after installing forever this package, still very easy. However, if the node project is built on a remote server, if it is not self-booting, it will be troublesome once the server restarts.


Can you use forever and startup scripting to solve these problems? The answer, of course, is yes, just a bit of a hassle, and the forever official lacks detailed configuration documentation. I also took some detours in the configuration, the following details.

Run node js forever 


Note: The experimental environment in this article is Ubuntu Server 12.04 LTS x86_64, which is simpler to configure on CentOS


How to run node server forever

First, I want to try to add a sentence in/etc/rc.local forever start xxx Look,forever monitor node js , the results found that Ubuntu (other systems like) do not bird me, the principal contradiction is MongoDB use this way can run up, forever will not, helpless under, Consider it from the perspective of/ETC/INIT.D.

Node forever


The premise is to first forever good, the method is very simple, execute the following command is good:


?
1 npminstallforever -g


Once the installation is complete, test it with a simple node program:


?
123 forever starttest.jsforever stoptest.jsforever restarttest.js


As long as the error is not prompted, it indicates that forever is available, 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 content of the script is as follows, thanks to the original author's hard work:


?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 #!/bin/bash### BEGIN INIT INFO# Provides:  xiyoulib# Required-Start: $all# Required-Stop: $all# Default-Start: 2 3 4 5# Default-Stop:  0 1 6# Short-Description: Start daemon at boot time# Description:  Enable service provided by daemon.### END INIT INFO# chkconfig: 345 88 08# description: Forever for Node.jsDEAMON=/node.js/XiyouLibNodeExpress/bin/www  #这里需要填写你自己的Node项目的启动脚本文件LOG=/node.js/log/log #可选,日志文件目录PID=/node.js/pid #必填内容,用于记录forever的进程号export PATH=$PATH:/usr/local/bin #在这里指定一下Node的可执行程序安装目录,我的是/usr/local/binexport NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules #这里是Node类库的路径#往下的内容就不用修改了node=nodeforever=forevercase "$1" 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 directory in the root directory for the node project, such as/node.js, and then set the permissions to 754, which avoids the hassle of some permissions problems!



Because of the Ubuntu server System, the above also configured the MongoDB start-up service, and in its INIT.D script added the following statement:


?
12 # Required-Start:  $all# Required-Stop:   $all


So in the future when you add the system will prompt the error, so in the node project launch script I added a previous string of notes, so that Ubuntu server system settings, if there should be no similar problems on CentOS, this should pay special attention!



That is, the following description information:


?
123456789 ### BEGIN INIT INFO# Provides:     xiyoulib# Required-Start:  $all# Required-Stop:   $all# Default-Start:   2 3 4 5# Default-Stop:   0 1 6# Short-Description: Start daemon at boot time# Description:    Enable service provided by daemon.### END INIT INFO


After editing the script, use the Chkconfig--list directive to see if the services you added are in effect, that is, 3 and 5 are all on to enable boot-up.



If 3 and 5 are not set to ON, then perform chkconfig--level 35 [your service name] on, Ubuntu server may report some warning, but as long as the required settings of the service 3, 5 to ON, Other errors can be ignored (I feel this is the system's own business).



After the setup is complete, the node project can be implemented on Linux self-booting, shutdown-r now try to self-start, start well after the direct access to your set port number, virtual directory God horse, if you want to do it!



But if not, check some scripts, and then make the relevant changes according to the error, after all, I also tried out!



Node Background Management plugin Forever


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.