1.
写一个脚本
-s
,能显示当前系统运行了多长时间
-m
,能显示出当前系统内存和
swap
的使用情况,包括易用空间和剩余空间
解:
先
top
一下看一看我们想要的结果:以下个字段的意识请看,续进程及命令博文
[root@zhangjixin tmp]# top
top - 08:18:40 up 1 day, 13:46, 4 users, load average: 0.25, 0.06, 0.02
Tasks: 153 total, 1 running, 123 sleeping, 28 stopped, 1 zombie
Cpu(s): 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1079944k total, 661924k used, 418020k free, 172484k buffers
Swap: 1048568k total, 0k used, 1048568k free, 314112k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 2064 624 532 S 0.0 0.1 0:01.18 init
#! /bin/bash
# top.sh :
#Author:our RHCE Team
#Create file time is 2010:06:11:02:49:39
#Version: 0.0.01
run_time(){
top -n 1 | grep 'day,' > /dev/null
if [ $? -eq 0 ];then #
为了防止时间不同可能影响到结果
M=`top -n 1 | grep 'top -' | cut -d' ' -f5-8 | cut -d, -f1-2`
else
M=`top -n 1 | grep 'top -' | cut -d' ' -f5-6 | cut -d, -f1`
fi
echo "This system is running time:$M"
}
ms_used(){
N=`top -n 1 | grep "^Mem:" | cut -d' ' -f8-9 | cut -d, -f1`
Q=`top -n 1 | grep "^Mem:" | cut -d' ' -f11-13 | cut -d, -f1`
R=`top -n 1 | grep "^Swap:" | cut -d' ' -f12-13 | cut -d, -f1`
S=`top -n 1 | grep "^Swap:" | cut -d' ' -f15-16 | cut -d, -f1`
echo "Now the system memery $N and $Q..."
echo "Now the system swap memery $R and $S..."
}
while getopts "sm" D
do
case $D in
s)
run_time ;;
m)
ms_used ;;
*)
echo "Usage:-s -m" ;;
esac
done
执行结果:
[root@zhangjixin tmp]# bash top.sh -s
This system is running time:1 day, 13:53
[root@zhangjixin tmp]# bash top.sh -m
Now the system memery 662916k used and 417028k free...
Now the system swap memery 0k used and 1048568k free...
2.
写一个脚本
能每隔一分钟检查一次网卡是否具有
IP
地址和子网掩码;
如果发现没有地址了,则尝试重启网络服务;
当收到
Ctrl+c
和
Ctrl+z
信号时,推出脚本(以某种错误状态码)
并返回用户一句话。
解:
#
!/bin/bash
# jiance.sh:
#autor : our RHCE team.
#Version : 0.0.1
jiance () {
while true
do
trap 'echo "you stopped it" && exit 5' INT
trap 'echo "you stopped it" && exit 5' STOP
ifconfig eth0|grep "inet addr"|cut -d: -f4 |head -1|cut -d' ' -f1 &> /dev/null
ifconfig eth0|grep "inet addr"|cut -d: -f2 |head -1|cut -d' ' -f1|grep ".*" &> /dev/null
if [ $? -ne 0 ];then
service network restart
else
sleep 10s
fi
done
}
jiance
~
~
~
运行结果: