#!/bin/bash
#Use:Startup script for the nginx HTTP Server
#Definition Global environment variable
IFS=$' \t\n'
unset -f unalias
\unalias -a
unset -f command
SYSPATH="$(command -p getconf PATH >/dev/null 2>&1)"
if [[ -z "$SYSPATH" ]];then
SYSPATH="/bin:/usr/bin"
fi
export PATH="$SYSPATH:$PATH"
#Definition nginx directory & service name
NGINX_DIR="/usr/local/webserver/nginx"
PROG="nginx"
#Definition check listen conflict function
check_listen(){
echo -n "Checek $PROG Listen port status: "
NGINX_LISTEN=(`grep "^listen" ${NGINX_DIR}/conf/$PROG.conf | awk '{print $NF}' | cut -d";" -f1 | uniq`)
SYSTEM_LISTEN=(`netstat -tnl |awk '{print $4}' | grep [0-9] | awk -F":" '{print $NF}' | uniq `)
for FIR in ${NGINX_LISTEN[*]}; do
for SEC in ${SYSTEM_LISTEN[*]};do
if [ $FIR == $SEC ]; then
echo "Has the listen port conflict, please check $PROG config file ${NGINX_DIR}/conf/$PROG.conf"
break
fi
done
done
echo "listen prot status OK!"
}
#definition check uid function
check_uid(){
if [ `id -u ` != "0" ];then
echo "Error,The Script must be root to run"
exit 1
fi
}
#Definition start function
start() {
check_uid
${NGINX_DIR}/sbin/$PROG -t >/dev/null 2>&1
if [ $? == "0" ]; then
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
echo -n $"Starting $PROG: "
echo "$PROG is already running"
exit 1
else
check_listen
${NGINX_DIR}/sbin/$PROG
echo -n $"Starting $PROG: "
echo "$PROG START OK"
fi
else
echo "$PROG syntax error,please check $PROG config file ${NGINX_DIR}/conf/$PROG.conf"
exit 2
fi
}
#Definition stop function
stop() {
check_uid
echo -n $"Stopping $PROG: "
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
pkill $PROG
echo "$PROG stop OK"
else
echo "$PROG process does not exist.so $PROG stop failed"
fi
}
#Definition reload function
reload() {
check_uid
echo -n $"Reloading $PROG: "
${NGINX_DIR}/sbin/$PROG -t >/dev/null 2>&1
if [ $? == "0" ]; then
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
kill -HUP `cat ${NGINX_DIR}/logs/$PROG.pid`
echo "$PROG reload OK"
exit 0
else
echo "$PROG process does not exist.so $PROG reload failed"
exit 1
fi
else
echo "$PROG syntax error,please check $PROG config file ${NGINX_DIR}/conf/$PROG.conf"
exit 2
fi
}
#Definition status function
status() {
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
echo "$PROG status is running"
else
echo "$PROG status is stop"
fi
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
configtest)
check_uid
${NGINX_DIR}/sbin/$PROG -t
;;
status)
status
;;
listentest)
check_uid
check_listen
;;
*)
echo $"Usage: $0{start|stop|restart|reload|configtest|status|listentest}"
exit 1
esac
#exit status code
exit $?
阅读(1946) | 评论(0) | 转发(0) |