


Use forever to implement self-starting of Node.js project on Linux_node.js
那么能否利用forever加启动脚本方式解决上述问题呢?答案当然是肯定的,只不过有点麻烦,而且forever官方缺少详细的配置文档。我在配置的时候也走了一些弯路,下面详细来说。
注:本文的实验环境是Ubuntu Server 12.04 LTS x86_64,在CentOS上的配置更简单一些
最早,我想着试试在/etc/rc.local中增加一句forever start xxx看看,结果发现Ubuntu(其他系统一样)就不鸟我,主要矛盾就是mongodb使用这种方式就可以跑起来,forever就不行,无奈之下,还是从/etc/init.d的角度去考虑吧。
前提是要先把forever好,方法很简单,执行如下命令就好:
npm install forever -g
安装完成后,用一个简单的Node程序测试一下:
forever start test.js forever stop test.js forever restart test.js
只要不提示error,就表明forever是可以用的,也就是说用forever来后台开启一个Node项目的基本条件已经具备,剩下的就是来编写一个启动脚本了。
脚本的基本内容如下,感谢原作者的辛勤劳动:
#!/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.js DEAMON=/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/bin export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules #这里是Node类库的路径 #往下的内容就不用修改了 node=node forever=forever case "$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
在这里提醒一下:最好为Node项目单独在根目录下建立一个目录,如/node.js,然后权限设为754,这样可以避免一些权限问题而造成的麻烦!
由于用的是Ubuntu Server系统,上面还配置了MongoDB的启动服务,而且在其init.d的脚本中加入了以下的语句:
# Required-Start: $all # Required-Stop: $all
所以在以后自己添加的时候系统会提示错误,所以在Node项目的启动脚本里我加了前面一串的说明注释,以便Ubuntu Server系统进行设置,如果在CentOS上,应该不会出现类似的问题,这点要特别注意!
即以下的说明信息:
### 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 --list指令查看一下自己添加的服务是否生效,即3和5全部要为on才可以实现开机自启动。
如果3和5没有被置为on,那么请执行chkconfig --level 35 [你的服务名] on即可,Ubuntu Server可能会报一些warning,但是只要能将所需设置的服务的3、5变为on,其他的错误可以忽略(我感觉这是系统自己的事)。
设置完成后即可实现Node项目在Linux上的自启动,可以shutdown -r now试一下能否自启,启动好以后直接去访问一下你设定的端口号、虚拟目录神马的,如果出来想要的就大功告成了!
但是如果不对,就好好检查一些脚本,然后根据报错进行相关修改,毕竟我也是试出来的嘛!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

How does Node.js do GC (garbage collection)? The following article will take you through it.

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!
