linux作业8

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

[root@localhost ~]# ps aux --sort=-%mem
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        711  0.0  1.4 357972 28972 ?        Ssl  04:46   0:00 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
root       1053  0.0  0.9 695044 19208 ?        Ssl  04:46   0:00 /usr/sbin/libvirtd
root       1047  0.0  0.9 573820 19200 ?        Ssl  04:46   0:00 /usr/bin/python -Es /usr/sbin/tuned -l -P
polkitd     635  0.0  0.7 542312 15160 ?        Ssl  04:46   0:00 /usr/lib/polkit-1/polkitd --no-debug
root        723  0.0  0.4 570160 10136 ?        Ssl  04:46   0:00 /usr/sbin/NetworkManager --no-daemon
root        390  0.0  0.4 200988  8156 ?        Ss   04:46   0:00 /usr/sbin/lvmetad -f
root          1  0.2  0.3 193688  6772 ?        Ss   04:46   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 22

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

# for循环
[root@localhost data]# cat ping_demo.sh 
#!/bin/bash

for i in `seq 255`
do
    ip=192.168.80.${i}

    ping  -c1 $ip  >& /dev/null
    if [ ! $? -eq 0 ];
    then
        echo "$ip is failed!!!"
    else
        echo "$ip is sucess!"
    fi
done

#while循环

[root@localhost data]# cat ping_while.sh 
#!/bin/bash

count=255
i=1
while [ $i -le $count ];
do
    ip=192.168.80.${i}
    ping -c1 $ip &> /dev/null
    if [ $? -eq 0 ];
    then
        echo "$ip is sucess!"
    else
        echo "$ip is failed!"
    fi
    let i++


done


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

### 先编写备份脚本
[root@localhost data]# cat backup_etc.sh 
#!/bin/bash

back_dir='/etc' 
dest_dir='/root/' 
back_date=`date -d "1 day ago" +"%Y-%m-%d-%H"`
back_name='etcbak-'$back_date
tar -cpJ $back_dir  -f ${back_name}.tar.xz &>/dev/null

mv ./${back_name}.tar.xz $dest_dir



### 再将脚本放入计划任务中执行
[root@localhost data]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
30 1 * * 1-5 root /data/backup_etc.sh


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

### 先编写备份脚本
[root@localhost data]# cat monitor_disk.sh 
#!/bin/bash
df -h |awk   '/^\/dev/ {print $1,int($5)}'  > /root/tmp_disk.txt

while read i ;
do
    PART=$(echo $i |cut -d" " -f1)
    USE=$(echo $i |cut -d" " -f2)
    if [ $USE -gt 20 ]; then
        echo "
        part: $PART
    use: $USE
    " | mail -s "monitor part used " root


    fi
done <  /root/tmp_disk.txt
### 脚本单独测试结果
[root@localhost data]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 2 messages 1 new
    1 root                  Tue Feb 18 06:18  22/650   "monitor part used"
>N  2 root                  Tue Feb 18 06:30  21/646   "monitor part used"
& 2
Message  2:
From [email protected]  Tue Feb 18 06:30:13 2020
Return-Path: 
X-Original-To: root
Delivered-To: [email protected]
Date: Tue, 18 Feb 2020 06:30:13 +0800
To: [email protected]
Subject: monitor part used 
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R


        part: /dev/sda1
        use: 30

###再将备份脚本放入计划任务执行

30 1 * * 1-5 root /data/back_etc.sh
*/10 *  *  *  1-5 root /data/monitor_disk.sh

你可能感兴趣的:(linux作业8)