#!/bin/bash
WHO=$(whoami)
if [ ${WHO} = root ]
then
:
else
echo -e “\033[1m\E[33;40mMust be root can run me !!!!\033[0m"
exit 1
fi
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Source networking configuration.
[ "$NETWORKING" = "no" ] && exit 0

NGINX_HOME=/home/hr/nginxhr
NGNIX_CONF=${NGINX_HOME}/conf/nginx.conf
NGINX_PID_FILE=${NGINX_HOME}/logs/nginx.pid
NGINX_BIN=${NGINX_HOME}/sbin/nginx
NGINX_NAME=$(basename ${NGINX_BIN})

lockfile=/var/lock/subsys/nginx_wp

Start(){
echo -n “start Nginx …”
[ -x $nginx ] || echo “Nginx no permit”
daemon ${NGINX_BIN}
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $lockfile
echo
return $RETVAL
}

Stop(){
PID=$(cat ${NGINX_HOME}/logs/*.pid)
echo -e “\033[1m\E[34;40mpid file give us ${NGINX_HOME} pid is :\033[0m\033[1m\E[33;40m${PID}\033[0m"
PS=$(/bin/ps -ef |grep ${NGINX_HOME}|grep -v grep|awk '{print $2}')
echo -e "\033[1m\E[34;40mps -ef give us ${NGINX_HOME} pid is :\033[0m\033[1m\E[33;40m${PS}\033[0m"
echo -e "\033[1m\E[33;40mdo you want to continue stop?yes or no\033[0m"
read -p "[yes|no]:” what
case ${what} in
yes) kill ${PID} && echo “${NGINX_HOME} is killed”;;
no) echo -e “\033[1m\E[33;41mthere must have some wrong\033[0m" && exit 1 ;;
*) echo -e "\033[1m\E[33;41mhey man,what are you doing here!!!\033[0m" && exit 1;;
esac
}

Reload(){
PID=$(cat ${NGINX_HOME}/logs/*.pid)
echo -e "\033[1m\E[34;40mpid file give us ${NGINX_HOME} pid is :\033[0m\033[1m\E[33;40m${PID}\033[0m"
PS=$(/bin/ps -ef |grep ${NGINX_HOME}|grep -v grep|awk '{print $2}')
echo -e "\033[1m\E[34;40mps -ef give us ${NGINX_HOME} pid is :\033[0m\033[1m\E[33;40m${PS}\033[0m"
echo -e "\033[1m\E[33;40mdo you want to continue reload?yes or no\033[0m"
read -p "[yes|no]:” load
case ${load} in
yes) kill -HUP ${PID};
newPID=$(/bin/ps -ef |grep ${NGINX_HOME}|grep -v grep|awk ‘{print $2}’);
echo -e “${NGINX_HOME} is reload ok ,PID is :\033[1m\E[31;40m$(echo ${newPID})\033[0m”;;
no) echo -e “\033[1m\E[33;41mthere must have some wrong\033[0m” && exit 1 ;;
*) echo -e “\033[1m\E[33;41mhey man,what are you doing here!!!\033[0m” && exit 1;;
esac

}

redo_log(){
PID=$(cat ${NGINX_HOME}/logs/*.pid);
kill -HUP ${PID};
}

Restart(){
Stop;
sleep 2;
Start;
}

Checkconfig(){
echo “checking Nginx config …”
${NGINX_BIN} -t
}
showhelp(){
echo “Usage: nginx.sh {start|stop|restart|checkconfig(c)}”
}

case $1 in
start)
Start
;;
stop)
Stop
;;
restart)
Restart
;;
reload)
Reload
;;
redolog)
redo_log
;;
checkconfig|c)
Checkconfig
;;
*)
showhelp
;;
esac