1.cd 到 /etc/init.d/下
2.创建nginx 文件 cat>>nginx 并编辑脚本
#! /bin/bash
# chkconfig: 35 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
set -e
PATH=/usr/local/nginx/sbin:/usr/local/nginx/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx web"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d_start(){
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON -s quit || echo -n " not running"
}
d_reload() {
$DAEMON -s reload || echo -n " counld not reload"
}
case "$1" in
start)
echo -n "Starting $DESC:$NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC:$NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
3.将shell脚本放入到 /etc/init.d/中,并执行下列命令:
3.1 设置可执行权限
chmod +x /etc/init.d/nginx
3.2 添加系统服务
chkconfig --add nginx
按照脚本中定义的命令就可以通过执行下列脚本达到执行nginx命令一样的效果:
#启动nginx
service nginx start
#停止运行nginx
service nginx stop
#重新加载重启nginx
service nginx reload
#重启nginx
service nginx restart