腳 本 練 習 題(四)

來源:互聯網
上載者:User

標籤:shell

1.按照這樣的日期格式(xxxx-xx-xx)每日產生一個檔案,例如今天產生的檔案為2013-09-23.log, 並且把磁碟的使用方式寫到到這個檔案中。

#! /bin/bashd=`date +%F`logfile=$d.logdf -h > $logfile

然後再增加一條每日定點執行的任務計劃即可,或(sleep 24*60*60)

2.ps 可以查看進程的記憶體佔用大小,寫一個指令碼計算一下所有進程所佔用記憶體大小的和。(提示,使用ps aux 列出所有進程,過濾出RSS那列,然後求和)

#! /bin/bashsum=0for mem in `ps aux |awk ‘{print $6}‘ |grep -v ‘RSS‘ `do    sum=$[$sum+$mem]doneecho "The total memory is $sum""k"

也可以使用awk 一條命令計算:

ps aux | grep -v ‘RSS TTY‘ |awk ‘{(sum=sum+$6)};END{print sum}‘

3.設計一個指令碼,監控遠端一台機器(假設ip為123.23.11.21)的存活狀態,當發現宕機時發一封郵件給你自己。

#! /bin/bashwhile : ;doping -c10 123.23.11.21 > 1.txt........sleep  30
提示:1. 你可以使用ping命令   ping -c10 www.baidu.com2. 發郵件的命令是  echo "郵件內容" |mail -s "主題" [email protected]3. 指令碼可以搞成死迴圈,每隔30s檢測一次

4.判斷原生80連接埠是否開啟著,如果開啟著什麼都不做,如果發現連接埠不存在,那麼重啟一下httpd服務,並發郵件通知你自己。指令碼寫好後,可以每一分鐘執行一次,也可以寫一個死迴圈的指令碼,30s檢測一次。

#port=80po=80#mail[email protected]while :;do        rs=`netstat -anlp|grep ‘:80‘ |wc -l`        if [ $rs -eq 0 ];then     #  a=`netstat -lnp |grep ":80" >/dev/null`     #  if [ -z $a ];then                echo "The port $po is die"|mail -s "message" $ma                service httpd start;        fi        sleep 30done
#! /bin/bash[email protected]if netstat -lnp |grep ‘:80‘ |grep -q ‘LISTEN‘; then    exitelse    /usr/local/apache2/bin/apachectl restart >/dev/null 2> /dev/null    echo "The 80 port is down."|mail -s ‘check_80‘ $mail    n=`ps aux |grep httpd|grep -cv grep`    if [ $n -eq 0 ]; then        /usr/local/apache2/bin/apachectl start 2>/tmp/apache_start.err    fi    if [ -s /tmp/apache_start.err ]; then        mail -s ‘apache_start_error‘ $mail < /tmp/apache_start.err    fifi


腳 本 練 習 題(四)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.