nginx的Shell启动脚本(启动nginx命令)

[root@shnne ~]# cat nginxd.sh 
#!/bin/bash
# chkconfig: 2345 58 80
# description: start and stop service for nginx
#定义的变量最好是大写.
nginx_bin="/application/nginx/sbin/nginx"
pid="/application/nginx1.6.2/logs/nginx.pid"
flag="/application/nginx1.6.2/logs/flag.log"

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
start() {
  if [ -f "$pid" ]
     then
       action "Nginx is running." /bin/false
     else
       $nginx_bin
       sleep 1 
       touch $flag
       action "Starting Nginx..." /bin/true 
  fi
}
stop() {
   if [ -f $pid ]
     then
       $nginx_bin -s stop
       sleep 1
       rm -f $flag
       action "Shutting down Nginx..." /bin/true
     else
       action "Nginx has been stopped." /bin/false
   fi
}
restart() {
     stop
     start
}

case "$1" in
start)
     start
     RETVAL=$?
     ;;
stop)
    stop
    RETVAL=$?
    ;;
restart)
    restart
    RETVAL=$?
    ;;
*)
   echo "USAGE:$0 {start|stop|restart}"
   ;;
esac
exit $RETVAL
if..elif..else(不是很完善)

[root@shnne ~]# cat start_web.sh 
#!/bin/sh
RETVAL=$1
num=`ps -ef|grep nginx|egrep -v "www|grep"|wc -l`
nginx_bin=/application/nginx/sbin/nginx
pid=/application/nginx1.6.2/logs/nginx.pid

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
start() {
  if [ "$num" -ge 2 ]
     then
       echo "Nginx is running"
       exit 0
     else
       $nginx_bin 
       action "Starting Nginx..." /bin/true 
  fi
}
stop() {
   if [ -f $pid ]
     then
       $nginx_bin -s stop
       action "Shutting down Nginx." /bin/true
     else
       echo "Nginx is stopping"
       exit 0
   fi
}
restart() {
   if [ "$num" -ge 2 ]
     then
      $nginx_bin -s stop
      action "Shutting down Nginx." /bin/true
      sleep 1
      $nginx_bin
      action "Starting Nginx..." /bin/true
   else
      action "Shutting down Nginx." /bin/false
      sleep 1
      $nginx_bin
      action "Starting Nginx..." /bin/true
   fi
}

if [ "$RETVAL" == "start" ]
 then
    start

elif [ "$RETVAL" == "stop" ]
 then
    stop
elif [ "$RETVAL" == "restart" ]
 then
    restart
else
   echo "USAGE:$0 {start|stop|restart}"
fi

标签:

上一篇通过Shell来监测MySQL状态
下一篇LAMP-php5.3.27配置环境(java环境变量配置)

相关文章