Shell脚本实例应用(系统管理)
时间:2010-11-07 来源:hanh422
为了持续观察服务器每天的运行状态,需要结合shell脚本程序和计划任务,定期跟踪记录不同时段服务器的cpu负载,内存,交换空间,磁盘使用量等信息。
实现手段:利用shell脚本程序
具体设计思路:
1.编写脚本monitor.sh
#!/bin/bash
#this is the second script!
day_time=`date+"%F %R"`
cpu_test=`uptime`
mem_test=`free -m | grep "mem" | awk '{print $2}'`
swap_test=`free -m | grep "mem" | awk '{print $4}'`
disk_test=`df -hT`
user_test=`last -n 10`
echo "now is $day_time"
echo "%cpu is $cpu_test"
echo "Numbet of Mem size(MB) is $mem_test"
echo "Number of swap size(MB) is $swap_test"
echo "the disk shiyong qingkuang is $disk_test"
echo "the users login qingkuang is $user_test"
2.设置cron任务
*/15 * * * * bash /monitor.sh
55 23 * * * tar cxf /var/log/runrec /var/log/running.today && --remove-files
备注:设置cron计划任务是为了时时执行此脚本,来监控系统状态,并记入日志,便于对系统更好的管理!
实现手段:利用shell脚本程序
具体设计思路:
1.编写脚本monitor.sh
#!/bin/bash
#this is the second script!
day_time=`date+"%F %R"`
cpu_test=`uptime`
mem_test=`free -m | grep "mem" | awk '{print $2}'`
swap_test=`free -m | grep "mem" | awk '{print $4}'`
disk_test=`df -hT`
user_test=`last -n 10`
echo "now is $day_time"
echo "%cpu is $cpu_test"
echo "Numbet of Mem size(MB) is $mem_test"
echo "Number of swap size(MB) is $swap_test"
echo "the disk shiyong qingkuang is $disk_test"
echo "the users login qingkuang is $user_test"
2.设置cron任务
*/15 * * * * bash /monitor.sh
55 23 * * * tar cxf /var/log/runrec /var/log/running.today && --remove-files
备注:设置cron计划任务是为了时时执行此脚本,来监控系统状态,并记入日志,便于对系统更好的管理!
相关阅读 更多 +