tengine是淘宝对于nginx1.6.2的一个二次开发,性能比原生态nginx更好,这几天在做测试,想应用到现有的架构里。源码包安装后就牵涉到一个添加到系统服务方便管理问题,到网上搜nginx启动脚本一大堆,但不是自己写的总归不放心,再者好多用的是系统的/etc/rc.d/init.d/funtions函数来实现的,虽然看着很健壮,但想到出了问题后又得研究怎么排查,一不做二不休干脆写一个,把脚本和添加到系统服务的过程分享一下。
功能主要有:service tengine start
service tengine stop
service tengine restart
service tengine reload
service tengine status
脚本程序如下(核心算法还是文件锁和"$?"的判断):
#!/bin/bash #writer:gaolixu #chkconfig: 345 86 16 start(){ if [ -f /var/lock/subsys/tengine.lock ];then echo "Tengine is already running: [ FAILED ]" else if /usr/local/nginx/sbin/nginx ;then echo "Starting tengine: [ OK ]" touch /var/lock/subsys/tengine.lock else echo "Starting tengine: [ FAILED ]" fi fi } stop(){ if [ -f /var/lock/subsys/tengine.lock ];then if /usr/local/nginx/sbin/nginx -s quit ;then echo "Stopping tengine: [ OK ]" rm -rf /var/lock/subsys/tengine.lock else echo "Stopping tengine: [ FAILED ]" fi else echo "Tengine not runing: [ FAILED ]" fi } reload(){ if /usr/local/nginx/sbin/nginx -s reload ;then echo "Reload tengine: [ OK ]" else echo "Reload tengine: [ FAILED ]" fi } case $1 in "start") start ;; "stop") stop ;; "restart") stop start ;; "reload") reload ;; "status") s=`pidof -s nginx` [ "$s" ] && echo "Tengine(nginx) pid $s running!!" || echo "Tengine(nginx) not runging!" ;; *) echo "usage: $0 start|stop|restart|reload|status" esac
将脚本做好后命名为tengine,放在/etc/init.d/目录下
chmod 777 tengine 将权限改成可执行
chkconfig --add tengine 添加到chkconfig服务管理中去
chkconfig tengine on 设置脚本注释(345)三个级别的开机自启动
好了,结束,测了一下,还是比较方便的。
时间: 2024-12-22 23:30:00