美文网首页
day53课堂笔记(if判断以及shell练习题)

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

作者: 五月_w | 来源:发表于2019-05-30 16:15 被阅读0次

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

    image.png image.png
    image.png

    第二章、if结构条件知识

    image.png image.png

    第三章、shell编程练习题

    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" 526434934@qq.com </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
    
    image.png

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

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

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/ttfetctx.html