第十一周作业

1、显示统计占用系统内存最多的进程,并排序。
## 按内存使用排序
[root@localhost ~]# ps -eo pid,ppid,%cpu,%mem,args --sort=-%mem |head -10
    PID    PPID %CPU %MEM COMMAND
    852       1  0.0  4.8 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
    849     824  0.0  4.7 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
    877       1  0.0  3.8 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
    822       1  0.0  2.7 /usr/lib/polkit-1/polkitd --no-debug
    867       1  0.0  2.4 /usr/sbin/NetworkManager --no-daemon
    848     824  0.0  1.8 /usr/libexec/sssd/sssd_be --domain implicit_files --uid 0 --gid 0 --logger=files
    824       1  0.0  1.7 /usr/sbin/sssd -i --logger=files
      1       0  0.0  1.6 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
   3116     878  0.0  1.3 sshd: root [priv]
[root@localhost ~]#

#按cpu 利用率排序
[root@localhost ~]# ps -eo pid,ppid,%cpu,%mem,args --sort=-%cpu |head -10
    PID    PPID %CPU %MEM COMMAND
   2935       2  0.1  0.0 [kworker/1:2-events_power_efficient]
      1       0  0.0  1.6 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
      2       0  0.0  0.0 [kthreadd]
      3       2  0.0  0.0 [rcu_gp]
      4       2  0.0  0.0 [rcu_par_gp]
      6       2  0.0  0.0 [kworker/0:0H-kblockd]
      8       2  0.0  0.0 [mm_percpu_wq]
      9       2  0.0  0.0 [ksoftirqd/0]
     10       2  0.0  0.0 [rcu_sched]
2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
### 使用for
[root@localhost shell]# cat ping_for.sh
#!/bin/bash

IP="192.168.0."
for ip in {2..254}
do
        {
                if `ping -c 3 $IP$ip > /dev/null`; then
                        echo "ping $IP$ip is success!!! "
                else
                        echo "ping $IP$ip is fail!!!"
                fi
        }&
done
wait
[root@localhost shell]#
[root@localhost shell]# ./ping_for.sh
ping 192.168.0.100 is success!!!
ping 192.168.0.103 is success!!!
ping 192.168.0.101 is success!!!
ping 192.168.0.2 is fail!!!
ping 192.168.0.4 is fail!!!
ping 192.168.0.7 is fail!!!
ping 192.168.0.8 is fail!!!

### 使用while
[root@localhost shell]# cat ping_while.sh
#!/bin/bash

IP="192.168.0."
declare -i ip=1
while [[ $ip -lt 255 ]]
do
        {
                if `ping -c 3 $IP$ip > /dev/null`; then
                        echo "ping $IP$ip is success!!!"
                else
                        echo "ping $IP$ip is fail!!!"
                fi
        }&
        let ip++
done
wait
[root@localhost shell]#
[root@localhost shell]# ./ping_while.sh
ping 192.168.0.1 is success!!!
ping 192.168.0.100 is success!!!
ping 192.168.0.101 is success!!!
ping 192.168.0.103 is success!!!
ping 192.168.0.104 is success!!!
ping 192.168.0.102 is success!!!
ping 192.168.0.3 is fail!!!
ping 192.168.0.8 is fail!!!
ping 192.168.0.5 is fail!!!
ping 192.168.0.9 is fail!!!
ping 192.168.0.13 is fail!!!
3、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@localhost shell]# ./etc_backup.sh
backuping file etcbak-2021-02-20-09.tar.xz
backup successed !!!
[root@localhost shell]# tree /opt/
/opt/
└── backup
    └── etcbak-2021-02-20-09.tar.xz

1 directory, 1 file
[root@localhost shell]# date
Sun Feb 21 09:02:37 EST 2021
[root@localhost shell]#
[root@localhost shell]# cat etc_backup.sh
#!/bin/bash

filename="etcbak-`date -d '- 1 day' +%F-%H`.tar.xz"
echo "backuping file $filename"
backup_path="/opt/backup"
[ -d $backup_path ]

if [ $? -ne 0 ]; then
        mkdir -p $backup_path
fi
cd $backup_path
`/usr/bin/tar czpPf $filename /etc/* &> /dev/null`

[ -f $filename ]

if [ $? -eq 0 ]; then
        echo "backup successed !!!"
else
        echo "backup action failed !!! please check"
fi

[root@localhost shell]#
[root@localhost ~]# crontab -l
## backup etc
30 1 * * 0 /bin/bash /root/shell/etc_backup.sh
[root@localhost ~]#
4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警。
[root@localhost shell]# cat check_disk.sh
#!/bin/bash
Disk=`df -TPh|awk '+$6>10 {print $1}'|tail -1`
if [[ -n $Disk ]];then
        echo "The part : $DISK will be full,please check !" | mail -s 'DISK WARNNING' root
fi
[root@localhost shell]#

[root@localhost shell]# ./check_disk.sh
[root@localhost shell]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 8 messages 1 new 2 unread
    1 root                  Sun Feb 21 09:38  22/892   "DISK WARNNING"
    2 root                  Sun Feb 21 09:38  22/892   "DISK WARNNING"
    3 root                  Sun Feb 21 09:38  22/892   "DISK WARNNING"
    4 root                  Sun Feb 21 09:38  22/892   "DISK WARNNING"
    5 root                  Sun Feb 21 09:38  22/892   "DISK WARNNING"
    6 root                  Sun Feb 21 09:38  22/902   "DISK WARNNING"
 U  7 root                  Sun Feb 21 09:38  22/901   "DISK WARNNING"
>N  8 root                  Sun Feb 21 09:39  21/881   "DISK WARNNING"
&
Message  8:
From [email protected]  Sun Feb 21 09:39:13 2021
Return-Path: 
From: root 
Date: Sun, 21 Feb 2021 09:39:13 -0500
To: [email protected]
Subject: DISK WARNNING
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
Status: R

The part :  will be full,please check !

&

你可能感兴趣的:(第十一周作业)