day53课堂笔记(if判断以及shell练习题)

第一章、整数二元比较操作符

16837788-e0772d23c9685ffe.png
image.png
16837788-12cf42c32c12835a.png
image.png

16837788-d92fb4fda7d2a68e.png
image.png

第二章、if结构条件知识

16837788-585da17a7d3b772d.png
image.png
16837788-a8cc6e27a2dc664d.png
image.png

第三章、shell编程练习题

16837788-543ba5160b3cb246.png
image.png

第一题:判断nginx是否活着,没有活着就发邮件报警,每三分钟执行一次

#!/bin/bash
n=`netstat -lntup |grep nginx |awk -F '[ :]+' '{print $5}'`
if [ "$n" != "80" ] 
then 
  `/application/nginx/sbin/nginx`
fi
m=`netstat -lntup |grep nginx |awk - F '[ :]+' '{print $5}'`
if [ "$m" = "80" ]
then
  echo "nginx is  running"
else 
   echo "nginx is not running"
   echo "nginx stop" |tee /tmp/status.log
   mailx -s "$date nginx dead" [email protected] </tmp/status.log
fi

第二题:三种方法监控nginx是否开启

#!/bin/bash
a=`lsof -i :80 |wc -l`
b=`netstat -lntup |grep nginx|wc -l`
if [ "$a" > "1" ] || [ "$b" > "0" ]
then
  echo "is running"
else
  `/application/nginx/sbin/nginx`
fi
c=`curl -s 10.0.0.7`
if [ $? -eq 0 ]
then 
   echo "is running"
else
   `/application/nginx/sbin/nginx`
fi

第三题:nginx实现开机自启动管理(start stop restart)

第一步:配置rsync.conf

cat>/etc/rsyncd.conf<<EOF
uid = rsync
gid = rsync
fake super = yes
use chroot = no
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = welcome to oldboyedu backup!
path = /backup/
EOF

第二步:写脚本(vim /etc/init.d/rsyncd,chmod +x /etc/init.d/rsyncd)

#!/bin/bash
#chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
if [ "$1" = "start" ]
then
   if [ -s /var/run/rsyncd.pid ]
   then
    :
   else
        rsync --daemon
   fi
elif [ "$1" = "stop" ]
then
   if [ -s /var/run/rsyncd.pid ]
   then
       kill `cat /var/run/rsyncd.pid`
   fi
elif [ "$1" = "restart" ]
then
   if [ -s /var/run/rsyncd.pid ]
   then
       kill `cat /var/run/rsyncd.pid`
   fi
    sleep 5
   if [ -s /var/run/rsyncd.pid ]
   then
       :
   else
       rsync --daemon
   fi
fi
if [ $# -ne 1 ]
then
    echo "usage; $0{start|stop|restat}"
    exit 1
fi
16837788-88fc887c0eeb1cf3.png
image.png

第三步:C6加入开机自启动步骤

chmod +x /etc/init.d/rsyncd
chkconfig --add rsyncd
chkconfig --list rsyncd
chkconfig  rsyncd  on (脚本中默认2345,开机和关闭顺序)
16837788-6181f19f439e67d8.png
image.png

16837788-61a6073353285b7c.png
image.png

16837788-54631c69d49ab614.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_34292924/article/details/90907502