Using forever to implement node. JS Project Self-start on Linux

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 and once the server restarts, the project must be manually opened by the administrator remotely to run.

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.


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 principal contradiction is MongoDB use this way can run up, forever will not, helpless under, Consider it from the perspective of/ETC/INIT.D.

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 simple, execute the following command is good:

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

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:

#!/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 #可选, log file directory Pid=/node.js/pid #必填内容, the process number used to record forever export path= $PATH:/usr/local/bin # Specify the executable program installation directory for NODE here, mine is/usr/local/binexport node_path= $NODE _path:/usr/local/lib/node_modules #这里是Node类库的路径 # The downward content does not have to modify the 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 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:

# 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:

# # # 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 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!


Original address: http://blog.csdn.net/yuanguozhengjust/article/details/37512993, reprint please indicate the source!

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.