您的位置:首页 > 运维架构 > Nginx

编写一个nginx开机启动脚本

2014-03-12 00:00 519 查看
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"
fi

;;
*)
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 start
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息