马哥Linux第八周

2020-04-12  本文已影响0人  Liang_JC

Q1、显示统计占用系统内存最多的进程,并排序。

[root@localhost ~]# ps -ef k -%mem | head -2
UID         PID   PPID  C STIME TTY      STAT   TIME CMD
root       6413      1  0 13:45 ?        Ssl    0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P

Q2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

[root@localhost ~]# cat ping-for.sh 
#!/bin/bash

NETID=192.168.0.
[ -f host_online.txt ] && rm -rf host_online.txt
for I in {1..254};do
    if ping -c1 -W1 ${NETID}$I &> /dev/null;then
        echo ${NETID}$I success | tee -a host_online.txt
    else
        echo ${NETID}$I fail
    fi &
done

[root@localhost ~]# cat ping-while.sh 
#!/bin/bash

NETID=192.168.0.
HOSTID=1
[ -f host_online.txt ] && rm -rf host_online.txt
while [ $HOSTID -le 254 ];do
    if ping -c1 -W1 ${NETID}$HOSTID &> /dev/null;then
        echo ${NETID}$HOSTID success | tee -a host_online.txt
    else
        echo ${NETID}$HOSTID fail
    fi &
    let HOSTID++
done

Q3、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间

[root@localhost ~]# crontab -e
30 1 * * 1-5 /usr/bin/tar -Jcf /backup/etcbak-`date +%F-%H`.tar.xz /etc

Q4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警

[root@localhost ~]# vim /data/checkdisk.sh
#!/bin/bash
SPACE=80
DISKSPACE=`df | sed -nr '/sd/s#.* ([0-9]+)%.*#\1#p' | sort -nr | head -1`

[ $DISKSPACE -ge $SPACE ] && echo "disk space is ge 80%" | mail -s "disk warning" root

[root@localhost ~]# crontab -e
*/10 * * * 1-5 /data/checkdisk.sh
上一篇 下一篇

猜你喜欢

热点阅读