一:编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次
[root@shell test]
free_mem=$( free - m | grep "Mem:" | tr - s " " | cut - d " " - f4)
if [ "$free_mem " -le 100 ] ; thenecho "剩余内存:${free_mem},不足100M " | mail - s "内存不足" root@shell
fi
[root@shell test]
[root@shell test]
* / 10 * * * * / test/free_mem. sh &> / dev/null
二:判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
[root@shell test]
nu=$( ps - ef | grep sshd | grep - v grep | wc - l)
if [ $nu -ge 1 ]
thenecho "sshd is running"
else echo "sshd is not running" systemctl start sshd > / dev/null
fi
[root@shell test]
[root@shell test]
sshd is running
三:检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)
[root@shell test]
ping - c 2 - w 1 192. 168. 10. 1 &> / dev/null
if [ $? -eq 0 ]
thenecho host is running
else echo host is not running
fi
[root@shell test]
[root@shell test]
host is not running
四:根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
read - p "please input your score:" score if [ - z "$score " ] ; then echo "you must input your score" exit 1fiif [ "$score " -lt 0 - o "$score " -gt 100 ] ; then echo "score invalid" exit 2i
fi
if [ "$score " -ge 85 ] ; then echo "A" elif [ "$score " -ge 70 ] ; then
echo "B" elif [ "$score " -ge 60 ] ; then
echo "C" else echo "D"
fi
[root@shell test]
[root@shell test]
[root@shell test]
please input your score:33
D
五:判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
[root@shell test]
vendor=$( grep "vendor_id" / proc/cpuinfo | uniq| awk - F: '{print $NF}' ) if [ [ "$vendor " =~ [ [ :space:] ] * GenuineIntel$ ] ]
thenecho "intel" elif [ [ "$vendor " =~ [ [ :space:] ] * AuthenticAMD$ ] ] then
echo "AMD" elseecho "unknow"
fi
[root@shell test]
[root@shell test]
intel