编写一个nginx开机启动脚本


1.编写脚本
[root@gyf  init.d]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: 2345 80 90
#description:  nginx
alter=$1
nginx=/usr/local/nginx/sbin/nginx
nginx_conf=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
. /etc/rc.d/init.d/functions
function if_info
{
        if [ $2 == 0 ];then
                echo -n "nginx $1 is ok!" && success && echo
        else
                echo -n "nginx $1 is error!" && success && echo
        fi
}
case $alter in
  start)
         if [ -f $nginx_pid ];then

                echo "nginx is already start!"
        else
                $nginx -c $nginx_conf
                if_info start $?
        fi
        ;;
  stop)
       if [ ! -f $nginx_pid ];then
                echo "nginx is already stop!"       
         else
                kill -TERM `cat $nginx_pid`

                if_info stop $?
        fi
        ;;
  restart)
        if [ ! -f $nginx_pid ];then
                echo "nginx is stop,please start nginx!"
        else
                kill -HUP `cat $nginx_pid`
                if_info restart $?
        fi
        ;;
test)
        $nginx -t -c $nginx_conf
#       $nginx -t
        if_info test $?
        ;;
  status)
        if [ ! -f $nginx_pid ];then
                echo "nginx is stop"
        else
                echo "nginx is runing"   
        finginx

        ;;
  *)
        echo "Usage: $0 {start|stop|status|restart|test}"
        ;;
esac

                                 
#!/bin/sh
#chkconfig: 2345 80 90
#description:auto_run
第一行,告诉系统使用的shell,因此的shell脚本都是这样。
第二行,chkconfig后面有三个参数2345,80和90告诉chkconfig程序,须要在rc2.d~rc5.d目录下,建立名字为 S80auto_run的文件链接,链接到/etc/rc.d/init.d目录下的的auto_run脚本。第一个字符是S,系统在启动的时候,运行脚 本auto_run,就会添加一个start参数,告诉脚本,如今是启动模式。同时在rc0.d和rc6.d目录下,建立名字为K90auto_run的 文件链接,第一个字符为K,个系统在关闭系统的时候,会运行auto_run,添加一个stop,告诉脚本,如今是关闭模式
                                  
2.将该文件设置为可执行文件

chmod  +x  /etc/init.d/nginx

3.添加指定的系统服务

chkconfig  --add  nginx

4.启动
 /etc/init.d/nginx startshell

相关文章
相关标签/搜索