第八周作业

空扰寡人 提交于 2020-02-14 21:53:31

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

 输入top命令后按shift+m

[root@db01 ~]# top
top - 20:54:39 up 17 days, 6 min, 5 users, load average: 0.00, 0.01, 0.05
Tasks: 212 total, 2 running, 210 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.1 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3881808 total, 1852912 free, 1063408 used, 965488 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 2500688 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
14108 root 20 0 1979088 234216 48872 S 0.0 6.0 10:42.50 gnome-shell
1348 mysql 20 0 2447264 140100 10580 S 0.0 3.6 15:39.62 mysqld
14282 root 20 0 1050424 101992 16468 S 0.0 2.6 0:48.66 gnome-soft+
14271 root 20 0 1100664 65072 20304 S 0.0 1.7 0:00.64 evolution-+
14445 root 20 0 1122012 61016 18828 S 0.0 1.6 0:00.22 evolution-+
14421 root 20 0 1202692 58940 18784 S 0.0 1.5 0:00.21 evolution-+
15820 root 20 0 940840 54720 21492 S 0.0 1.4 0:01.41 system-con+
14301 root 39 19 805688 50812 8796 S 0.0 1.3 0:01.86 tracker-ex+

 

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

 fo语句实现:

#/bin/bash

#

for i in {1..254};do
if ping -W 1 -c 1 192.168.0.$i &> /dev/null; then
echo "192.168.0.$i is up,success!"
else
echo "192.168.0.$i is down,failed!"
fi
done
[root@db01 scripts]# bash -n fping.sh
[root@db01 scripts]# bash fping.sh
192.168.0.1 is down,failed!
192.168.0.2 is down,failed!
192.168.0.3 is up,success!
192.168.0.4 is down,failed!

 

while实现:

#/bin/bash

#

declare -i i=1
while [ $i -le 254 ];do
if ping -W 1 -c 1 192.168.0.$i &> /dev/null; then
echo "192.168.0.$i is up,success!"
else
echo "192.168.0.$i is down,failed!"
fi
let i++
done

[root@db01 scripts]# bash -n wping.sh
[root@db01 scripts]# bash wping.sh
192.168.0.1 is down,failed!
192.168.0.2 is down,failed!
192.168.0.3 is up,success!
192.168.0.4 is down,failed!
192.168.0.5 is up,success!
192.168.0.6 is down,failed!
192.168.0.7 is down,failed!

 

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

 1 编写备份脚本:
 2 
 3 [root@db01 backup]# ls
 4 [root@db01 backup]# cd ~/scripts/
 5 [root@db01 scripts]# vim backup.sh
 6 
 7 #!/bin/bash
 8 #
 9 
10 tar -Jcf /backup/etcbak-`date -d "1 day ago" +"%F-%H"`.tar.xz /etc
11 
12 执行脚本测试:
13 
14 [root@db01 scripts]# bash backup.sh
15 tar: Removing leading `/' from member names
16 [root@db01 scripts]# cd /backup/
17 [root@db01 backup]# ls
18 etcbak-2020-02-13-20.tar.xz
19 
20 [root@db01 backup]# rm etcbak-2020-02-13-20.tar.xz
21 rm: remove regular file ‘etcbak-2020-02-13-20.tar.xz’? y
22 
23 编写计划任务:
24 
25 [root@db01 backup]# crontab -e
26 
27 [root@db01 backup]# crontab -l
28 30 1 * * 1-5 /usr/bin/bash ~/scripts/backup.sh

 

 

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

 1 编写检查脚本,并发邮件。
 2   
 3 [root@db01 scripts]# vim diskcheck.sh
 4 #!/bin/bash
 5 #
 6 
 7 DISK=`df -TPh|awk '+$6>80 {print $1}'|tail -1`
 8 if [ ! -z $DISK ];then
 9         echo "The part : $DISK will be full,please check !" | mail -s DISK WARNNING' root
10 fi
11 
12 测试脚本,本次测试磁盘使用率告警阈值为15%:
13 
14 [root@db01 backup]# bash ~/scripts/diskcheck.sh
15 [root@db01 backup]# mail
16 Heirloom Mail version 12.5 7/5/10.  Type ? for help.
17 "/var/spool/mail/root": 23 messages 2 new 6 unread
18  U 21 root                  Fri Feb 14 20:28  19/629   "DISK WARNNING"
19 >N 22 root                  Fri Feb 14 20:32  18/619   "DISK WARNNING"
20  N 23 root                  Fri Feb 14 20:32  18/619   "DISK WARNNING"
21 & 23
22 Message 23:
23 From root@db01.localdomain  Fri Feb 14 20:32:33 2020
24 Return-Path: <root@db01.localdomain>
25 X-Original-To: root
26 Delivered-To: root@db01.localdomain
27 Date: Fri, 14 Feb 2020 20:32:33 +0800
28 To: root@db01.localdomain
29 Subject: DISK WARNNING
30 User-Agent: Heirloom mailx 12.5 7/5/10
31 Content-Type: text/plain; charset=us-ascii
32 From: root@db01.localdomain (root)
33 Status: R
34 
35 The part : /dev/sda1 will be full,please check !
36 
37 编写计划任务:
38 
39 [root@db01 backup]# crontab -e
40 [root@db01 backup]# crontab -l
41 */10 * * * 1-5 /usr/bin/bash ~/scripts/diskcheck.sh

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!