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

一、安装nginx
http://blog.youkuaiyun.com/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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值