1、显示统计占用系统内存最多的进程,并排序。
[root@localhost ~]# ps axo pid,cmd,%cpu,%mem --sort -%mem
PID CMD %CPU %MEM
5516 /opt/py3/bin/python3.6 /opt 0.0 1.8
5514 /opt/py3/bin/python3.6 /opt 0.0 1.7
5510 /opt/py3/bin/python3.6 /opt 0.0 1.7
5546 /opt/py3/bin/python3.6 /opt 0.1 1.7
5512 /opt/py3/bin/python3.6 /opt 0.0 1.6
5561 /opt/py3/bin/python3.6 /opt 0.0 1.6
5518 /opt/py3/bin/python3.6 /opt 0.2 1.6
5534 /opt/py3/bin/python3.6 /opt 0.2 1.6
5562 /opt/py3/bin/python3.6 /opt 0.0 1.6
2776 /usr/libexec/mysqld --based 0.1 1.6
5538 /opt/py3/bin/python3.6 /opt 0.0 1.6
5556 /opt/py3/bin/python3.6 /opt 0.0 1.5
5557 /opt/py3/bin/python3.6 /opt 0.0 1.5
。。。。。。
2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
#for循环
[root@localhost ~]# cat for_ping.sh
#!/bin/bash
NET=192.168.0
for ID in {1..254};do
{
ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID "success!" || echo $NET.$ID "fail!"
}&
done
wait
#while循环
[root@localhost ~]# cat while_ping.sh
#!/bin/bash
NET=192.168.0.
declare -i Id=1
while [ $Id -le 254 ];do
{
ping -c1 -W1 $NET$Id > /dev/null
if [ $? -eq 0 ];then
echo "$NET$Id success!"
else
echo "$NET$Id fail!"
fi
} &
let Id++
done
wait
3、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@localhost ~]# cat etcbackup.sh
#!/bin/bash
[ -d /backup ] || mkdir /backup
/usr/bin/tar -JcPf /backup/etcbak-`date -d -1day +%F-%H`.tar.xz /etc/ > /dev/null
[root@localhost ~]# crontab -l
30 1 * * 1-5 /bin/bash /root/etcbackup.sh
[root@localhost ~]# ll /backup/
总用量 13704
-rw-r--r-- 1 root root 7012436 2月 5 13:48 etcbak-2021-02-04-13.tar.xz
-rw-r--r-- 1 root root 7012436 2月 5 14:00 etcbak-2021-02-04-14.tar.xz
4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警
#配置邮箱
[root@localhost ~]# cat .mailrc
set from=1787183478@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1787183478@qq.com
set smtp-auth-password=pzzxrfltahtudddh
set smtp-auth=login
set ssl-verify=ignore
[root@localhost ~]# cat diskcheck.sh
#!/bin/bash
WARNING=80
df | sed -En '/^\/dev\/sd/s@^([^ ]+).* ([0-9]+)%.*@\1 \2@p'| while read DEVICE USE;
do
[ $USE -gt $WARNING ] && echo "$DEVICE will be full,USE:$USE" | mail -s "diskfull" 1787183478@qq.com
done
[root@localhost ~]# crontab -l
*/10 * * * 1-5 /bin/bash /root/diskcheck.sh