shell学习之十八--nginx启动脚本(if)

来源:互联网 发布:淘宝卖lol限定皮肤 编辑:程序博客网 时间:2024/06/05 10:45
一、安装nginx
http://blog.csdn.net/yujin2010good/article/details/51637912

二、编写脚本(使用if编写nginx启动脚本)
1、编写初步脚本
[root@node01 day7]# vi nginx_stat.sh 
#!/bin/sh
. /etc/init.d/functions
if [ $# -ne 1 ]
   then
      echo "USAGE $() {start|stop|restart}"
      exit 1
fi


if [ "$1" == "start" ]
   then 
      action "start nginx" /bin/true
elif [ "$1" == "stop" ]
   then
      action "stop nginx" /bin/true
elif [ "$1" == "restart" ]
   then
      action "restart nginx" /bin/true
else
      echo "USAGE $() {start|stop|restart}"
      exit 1
fi
[root@node01 day7]# sh nginx_stat.sh start
start nginx                                                [  OK  ]
[root@node01 day7]# sh nginx_stat.sh stop
stop nginx                                                 [  OK  ]
[root@node01 day7]# sh nginx_stat.sh start
start nginx                                                [  OK  ]
[root@node01 day7]# sh nginx_stat.sh
USAGE  {start|stop|restart}
[root@node01 day7]#

2、优化脚本(代入函数)
#!/bin/sh
. /etc/init.d/functions
USAGE(){
     echo "USAGE $() {start|stop|restart}"
     exit 1
}
if [ $# -ne 1 ]
   then
      USAGE
fi


if [ "$1" == "start" ]
   then 
      action "start nginx" /bin/true
elif [ "$1" == "stop" ]
   then
      action "stop nginx" /bin/true
elif [ "$1" == "restart" ]
   then
      action "restart nginx" /bin/true
else
    USAGE
fi
[root@node01 day7]# sh nginx_stat02.sh 
USAGE  {start|stop|restart}
[root@node01 day7]# sh nginx_stat02.sh start
start nginx                                                [  OK  ]
[root@node01 day7]# sh nginx_stat02.sh stop
stop nginx                                                 [  OK  ]
[root@node01 day7]# sh nginx_stat02.sh restart
restart nginx                [  OK  ]
[root@node01 day7]# sh nginx_stat02.sh restart fd kjk 121
USAGE  {start|stop|restart}

3、加入真正操作
#!/bin/sh
. /etc/init.d/functions
start_nginx=/soft/nginx-1.8.1/objs/nginx
USAGE(){
     echo "USAGE $() {start|stop|restart}"
     exit 1
}
if [ $# -ne 1 ]
   then
      USAGE
fi


if [ "$1" == "start" ]
   then
      $start_nginx   
      action "start nginx" /bin/true
elif [ "$1" == "stop" ]
   then
      killall nginx
      action "stop nginx" /bin/true
elif [ "$1" == "restart" ]
   then
      pkill nginx
      sleep 2
      $start_nginx  
      action "restart nginx" /bin/true
else
    USAGE
fi
[root@node01 day7]# sh nginx_start03.sh start
start nginx                                                [  OK  ]
[root@node01 day7]# ps -ef |grep nginx
root      73948      1  0 23:33 ?        00:00:00 nginx: master process /soft/nginx-1.8.1/objs/nginx
nginx     73950  73948  0 23:33 ?        00:00:00 nginx: worker process       
root      73952  56270  0 23:33 pts/4    00:00:00 grep nginx
[root@node01 day7]# sh nginx_start03.sh restart
restart nginx                                              [  OK  ]
[root@node01 day7]# sh nginx_start03.sh stop
stop nginx                                                 [  OK  ]
[root@node01 day7]# ps -ef |grep nginx      
root      74026  56270  0 23:34 pts/4    00:00:00 grep nginx
[root@node01 day7]# ps -ef |grep nginx
root      74028  56270  0 23:34 pts/4    00:00:00 grep nginx
原创粉丝点击