本文来写一个Nginx-1.5.4的守护进程的Shell脚本,其它版本的Nginx与此脚本类似。
我们把Nginx-1.5.4安装到了/usr/lcoal/nginx/目录下,在sbin目录下一个名为nginx的可执行程序。如果执行此命令,将启动Nginx守护进程。如果使用-s quit,可以停止Nginx;如果使用-s reload可以重新载入Nginx的配置文件。我们把这三个方法来按照守护进程的标准格式封装成start、stop、reload、restart这四个操作。
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
# If the daemon file is not found, terminate the script.
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 " could 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 "Reload $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for 2 seconds to give $DESC some time to perform a graceful stop.
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
脚本到此结束,这个脚本大家可以尽情的抄,复制到后台,我们拿它来把Nginx-1.5.4安装成Linux服务。