第十一周练习

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

-   ps aux | sort -k4nr| head -1
-   ps aux --sort=-%mem | head -3
-   ps aux --sort=-rsz | head -3
-   top +M

1.ps显示当前终端进程信息

ps 即process state,可以显示当前进程状态的快照,默认显示当前终端中的进程,Linux系统各进程的相关信息均保存在/proc/PID目录下的各文件中

使用选项:

  • a 选项包括所有终端中的进程
  • u 选项显示进程所有者的信息
  • x 选项包括不链接终端的进程
[root@centos8 ~]# ps aux | sort -k4nr| head -1
root         891  0.0  1.8 431104 38040 ?        S    Jun30   0:01 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files

sort 文本排序:
把整理过的文本显示在STDOUT,不改变原始文件
-r	执行反方向(由上至下)整理  
-n   执行按数字大小整理  

[root@centos8 ~]# ps aux --sort=-rsz | head -3
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         891  0.0  1.8 431104 38040 ?        S    Jun30   0:01 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
root         878  0.0  1.5 635036 31096 ?        Ssl  Jun30   0:09 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P

[root@centos8 ~]# ps aux --sort=-%mem | head -3
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         891  0.0  1.8 431104 38040 ?        S    Jun30   0:01 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
root         878  0.0  1.5 635036 31096 ?        Ssl  Jun30   0:09 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P

[root@centos8 ~]# ps aux --sort -rss |head -2
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         891  0.0  1.8 431104 38040 ?        S    Jun30   0:01 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files

2.top 提供动态的实时进程

top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器。

直接使用top命令后,查看%MEM的内容。按M键,根据内存使用百分比进行排序。

  • %MEM:进程使用的物理内存和总内存的百分比
[root@centos8 ~]# top
top - 11:43:51 up 12:08,  2 users,  load average: 0.00, 0.02, 0.03
Tasks: 213 total,   2 running, 209 sleeping,   2 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   1960.1 total,   1107.2 free,    289.2 used,    563.6 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.   1500.3 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                    
    891 root      20   0  431104  38040  36388 S   0.0   1.9   0:01.41 sssd_nss                   
    878 root      20   0  635036  31096  16296 S   0.0   1.5   0:09.04 tuned                      
    859 polkitd   20   0 1761116  21892  16988 S   0.0   1.1   0:00.22 polkitd                    
   4408 root      20   0  390268  21312  12272 T   0.0   1.1   0:00.36 python3                    
   4383 root      20   0  390272  20876  12004 T   0.0   1.0   0:00.28 python3                    
    861 root      20   0  600068  17796  15520 S   0.0   0.9   0:02.00 NetworkManager             
    886 root      20   0  428900  14884  12200 S   0.0   0.7   0:01.12 sssd_be                    


常用的命令:
P:以占据的CPU百分比,%CPU
M:占据内存百分比,%MEM
T:累积占据CPU时长,TIME+

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

  • for:依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束

    说明:当前本机网段是10.0.0.0/24网段,所以用10.0.0.0/24代替192.168.0.0/24网段

[root@centos8 scripts]# cat ip_scan_for.sh 
#!/bin/bash
#
#******************************************************
#Author:		haha
#QQ: 			906195954
#Date: 			2021-07-01
#FileName:		/data/scripts/ip_scan_for.sh
#URL: 			https://www.csdn.net
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#******************************************

NET=10.0.0
for ID in {1..254};do
	{
	ping -c1 -w1 $NET.$ID &> /dev/null && echo $NET.$ID is success! || echo $NET.$ID is faild!
	}&
done
wait

&   #并发执行

[root@centos8 scripts]# cat ip_scan_for2.sh 
#!/bin/bash
#
#******************************************************
#Author:		haha
#QQ: 			906195954
#Date: 			2021-07-01
#FileName:		/data/scripts/ip_scan_for.sh
#URL: 			https://www.csdn.net
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#******************************************

NET=10.0.0
for ID in {1..254};do
	{
	if ping -c1 -w1 $NET.$ID &> /dev/null;then
	
	echo $NET.$ID is success! 
	
else
	echo $NET.$ID is faild!
	
	fi

	}&
done
wait


测试:
[root@centos8 scripts]# bash ip_scan_for.sh 
10.0.0.2 is success!
10.0.0.18 is success!
10.0.0.17 is success!
10.0.0.102 is success!
10.0.0.5 is faild!
10.0.0.3 is faild!
10.0.0.4 is faild!
10.0.0.1 is faild!
10.0.0.10 is faild!
*******************
10.0.0.254 is faild!
10.0.0.248 is faild!
  • while

    CONDITION:循环控制条件;进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”,则执行一次循环;直到条件测试状态为“false”终止循环,因此:CONDTION一般应该有循环控制变量;而此变量的值会在循环体不断地被修正

[root@centos8 scripts]# cat ip_scan_while.sh 
#!/bin/bash
#
#******************************************************
#Author:		haha
#QQ: 			906195954
#Date: 			2021-07-01
#FileName:		ip_scan_while.sh
#URL: 			https://www.csdn.net
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#******************************************
touch /data/ip.txt
echo 10.0.0.{1..254} |tr -s " " "\n" > /data/ip.txt
while read ip;do
{
	ping -c1 -w1 $ip >/dev/null
	if [ $? -eq 0 ];then
		echo $ip is up
	else
		echo $ip is down
	fi
}&
done </data/ip.txt
wait


[root@centos8 scripts]# cat ip_scan_while2.sh 
#!/bin/bash
#
#******************************************************
#Author:		haha
#QQ: 			906195954
#Date: 			2021-07-01
#FileName:		ip_scan_while2.sh
#URL: 			https://www.csdn.net
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#******************************************
#定义变量i控制循环次数,i从1开始循环,每循环一次,i自加1,直到i等于254循环退出
#ping命令的-c选项控制ping测试的次数,c1表示对目标主机执行2次ping测试
#ping命令的-W选项,可以控制超时时间,使用-W1,设置超时时间为1秒
i=1
while [ $i -le 254 ]
do
{
	ping -c1 -w1 10.0.0.$i &> /dev/null
	if [ $? -eq 0 ];then
		echo 10.0.0.$i is up
	else
		echo 10.0.0.$i id down
	fi
}&
	let i++
done
wait


[root@centos8 scripts]# bash ip_scan_while2.sh
10.0.0.2 is up
10.0.0.18 is up
10.0.0.17 is up
10.0.0.102 is up
10.0.0.14 id down
10.0.0.1 id down
10.0.0.4 id down
10.0.0.11 id down

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

#创建脚本etc_backup.sh 
[root@centos8 scripts]# cat etc_backup.sh 
#!/bin/bash
#
#******************************************************
#Author:		haha
#QQ: 			906195954
#Date: 			2021-07-01
#FileName:		etc_backup.sh
#URL: 			https://www.csdn.net
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#******************************************
#将/etc备份至/backup目录中,保存的文件名称格式 为"etcbak-yyyy-mm-dd-HH.tar.xz",其中日期是前一天的时间
[ -d/backup ] || mkdir /backup
tar cJvfP /backup/etcbak-`date -d "-1day" +%F_%T`.tar.xz  /etc 

[root@centos8 scripts]# ll /etc/crontab 
-rw-r--r--. 1 root root 492 Jul  2 00:59 /etc/crontab
[root@centos8 scripts]# 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 /data/scripts/etc_backup.sh

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

[root@centos8 scripts]# cat checkdisk.sh 
#!/bin/bash
#
#******************************************************
#Author:		haha
#QQ: 			906195954
#Date: 			2021-07-02
#FileName:		checkdisk.sh
#URL: 			https://www.csdn.net
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#******************************************
WARNING=80
df | awk -F ' +|%' '/^\/dev\/sd/{print $1,$5}'|while read DISK USE;do
if [ $USE -gt $WARNING ];then
	echo "`hostname -I` $DISK disk space will be full,use:$USE%" | mail -s diskwarning n51@localhost
	fi
	done
[root@centos8 scripts]# 

测试
#/boot分区下创建文件,将/boot分区使用率超过80%
[root@centos8 scripts]# dd if=/dev/zero of=/boot/testfile bs=1M count=800
800+0 records in
800+0 records out
838860800 bytes (839 MB, 800 MiB) copied, 11.8106 s, 71.0 MB/s
[root@centos8 scripts]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        964M     0  964M   0% /dev
tmpfs           981M     0  981M   0% /dev/shm
tmpfs           981M  8.7M  972M   1% /run
tmpfs           981M     0  981M   0% /sys/fs/cgroup
/dev/sda2       100G  2.5G   98G   3% /
/dev/sda5        50G  390M   50G   1% /data
/dev/sda1       976M  904M  5.4M 100% /boot
tmpfs           197M     0  197M   0% /run/user/0
/dev/sr0        7.7G  7.7G     0 100% /misc/cd

#更改计划任务的检测时间为1分钟
[root@centos8 scripts]# crontab -l
*/1 * * * * /data/scripts/checkdisk.sh


[root@centos8 ~]# su - n51
[n51@centos8 ~]$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/n51": 1 message 1 new
>N  1 root                  Fri Jul  2 02:00  18/609   "diskwarning"
& 1
Message  1:
From root@centos8.localdomain  Fri Jul  2 02:00:04 2021
Return-Path: <root@centos8.localdomain>
X-Original-To: n51@localhost
Delivered-To: n51@localhost
Date: Fri, 02 Jul 2021 02:00:03 +0800
To: n51@localhost
Subject: diskwarning
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root <root@centos8.localdomain>
Status: R

10.0.0.18 /dev/sda1 disk space will be full, used:100%

& 


上一篇:Linux基础:VMWare虚拟机安装CentOS后配置静态ip


下一篇:Hyperledger Fabric教程(8)--byfn.sh分析-script.sh