最近在學shell腳本開發,然后就想寫一個簡單的監控腳本,然后做成定時任務,期間碰到了定時任務不生效的問題,在這里統一記錄下來
shell腳本如下:
#!/bin/bash
#獲取ip地址
#ip=`ifconfig eth0 | grep "inet" | cut -f 2 -d ":"`
#獲取系統總核數
#cpu_num=`grep -c 'model name' /proc/cpuinfo`
#cpu_num=grep -c 'cpu cores' /proc/cpuinfo
#獲取當前時間
now=`date -u -d"+8 hour" +'%Y-%m-%d %H:%M:%S'`
#cpt使用閾值
cpu_warn='75'
#mem空閑閾值
mem_warn='100'
#disk使用閾值
disk_warn='90'
#------cpu
function item_cpu(){
cpu_idle=` top -b -n 1 | grep Cpu |awk {'print $8'}|cut -f 1 -d "."`
#cpu使用率
cpu_use=`expr 100 - $cpu_idle`
echo "$now 當前的cpu使用率為 $cpu_use" >> /linuxTest/cpu.log
if [[ $cpu_use -gt $cpu_warn ]]; then
echo "cpu報警" >> /linuxTest/cpu.log #這里的檔案型別要寫成絕對路徑,要不然定時任務會不生效
else
echo "cpu使用正常" >> /linuxTest/cpu.log
fi
}
#----mem記憶體
function item_mem(){
mem_free=`free -m |grep "Mem"| awk {'print $4+$6'}`
echo "$now 當前記憶體剩余空間為 $mem_freeMB" >> /linuxTest/mem.log
if [[ $mem_free -lt $mem_warn ]]; then
echo "mem報警" >> /linuxTest/mem.log
else
echo "mem使用正常" >> /linuxTest/mem.log
fi
}
#----disk磁盤
function item_disk(){
disk_use=`df -P | grep /dev | grep -v -E '(tmp|boot)' | awk '{print $5}' | cut -f 1 -d "%"`
echo "$now 當前磁盤使用率為 $disk_use %" >> /linuxTest/disk.log
if [[ $disk_use -gt $disk_warn ]]; then
echo "disk報警" >> /linuxTest/disk.log
else
echo "disk使用正常" >> /linuxTest/disk.log
fi
}
item_cpu
item_disk
item_mem
寫完shell腳本之后就想這跑個定時任務
命令是:crontab -e
然后就會進入到定時任務的撰寫中(有關定時任務的引數可以自己百度)
我寫了一個每分鐘都執行的測驗案例
命令是:* * * * * /linuxTest/cpuWatch.sh
然后寫完命令后重啟crond的服務,發現這個定時任務一直都沒有跑起來,去查了一下資料后,設定了幾個引數
1.首先 tail -100f /var/log/messages 查看系統運行的日志,發現了
crond: sendmail: fatal: parameter inet_interfaces: no local interface found for ::1,解決完這個問題后,還是沒有跑起來
2.然后把日志的檔案路徑寫成了絕對路徑后,就可以跑起來了

嘿嘿,解決問題的感徑訓是很爽的!雖然是個小問題,可是這種成就感還是很好的
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/216204.html
標籤:其他
上一篇:awk 學習
下一篇:【03】Nginx的組態檔介紹
