使用chkconfig
配置Node.js程度的开机启动,相比将启动命令写入rc.local
文件可配置性更强。chkconfig
命令可以配置服务的运行级别,及在不同运行级别下的启动顺序。
chkconfig
配置开机服务有以下几个步骤:
下面以配置itbilu.com
及另一个站点的开机启动服务为例,简述chkconfig
的配置过程:
1. 编写启动脚本
#!/bin/bash # chkconfig: 2345 80 90 # description: sites service PATH=/usr/local/bin/ ITBILU=/home/www/itbilu/app.js OTHER=/home/www/other/app.js start() { echo "sites starting ..." # 各站点依次启动 forever start -u itbilu -a $ITBILU & forever start start -u other -a $OTHER } stop() { echo "sites stoping ..." # 停止各站点 forever stop $ITBILU & forever stop $OTHER } restart() { echo "sites restarting ..." # 全部重启 forever restartall } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart|force-reload) restart ;; *) echo $"Usage: $0 {start|stop|restart|force-reload}" exit 1 ;; esac
在上面脚本中,配置了针对forever
命令的,多站点启动、停止、重启服务。在启动程序时,添加了-a
参数,该参数表示向原有日志中继续追加日志。将以上脚本保存为名sites
的文件。
2. 将脚本放入/etc/init.d
目录,并修改脚本权限
Linux在系统启动,初始化完成后,会启动/etc/init.d
目录下各个运行级别脚本。前面的Node.js程序启动脚本也需要放到这个目录下:
cp ./sites /etc/init.d/sites
修改脚本权限:
chmod +x /etc/init.d/sites
3. chkconfig
配置运行级别
使用chkconfig
命令将sites
添加为服务,并开启服务:
chkconfig --add sites chkconfig sites on
配置完成,reboot
重启测试……