linux 开机自启动 nginx
if [ -z $1 ]
then
echo "Usage: $0 {start|stop|reload|restart|status}"
exit 1
fi
#service name for nginx
SNAME=nginx
#nginx home dir
NGINX_HOME=/usr/local/nginx
PID=$NGINX_HOME/logs/nginx.pid
#nginx running port
NGINX_PORT='netstat -anp|grep -w 80|grep LISTEN|grep -v TIME_WAIT|wc -l'
#the full path of daemon
NGINX_PROG=$NGINX_HOME/sbin/nginx
#start function
start(){
#check the daemon status first
if [ $NGINX_PORT -ne 0 ]
then
echo "WARN: $SNAME is already started"
exit 0;
else
$NGINX_PROG
echo "starting $SNAME .....................done"
exit 0;
fi
}
#stop function
stop(){
if [ $NGINX_PORT = 0 ]
then
echo "WARN: $SNAME has been stopped"
else
$NGINX_PROG -s quit
echo "stopping $SNAME..........................done"
fi
}
#reload function
restart(){
if [ -f $PID ]
then
$NGINX_PROG -s reload
echo "restart $SNAME .......................done"
else
echo "$PID is not exist!"
$NGINX_PROG
echo "start $SNAME ......................... done"
fi
}
status(){
if [ $NGINX_PORT = 0 ]
then
echo "$SNAME has already stopped!"
else
echo "$SNAME is running ok!"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload|restart)
restart
;;
status)
status
;;
*)
echo "usage: $0 {start|stop|reload|restart|status}"
exit 1
esac
....