1、显示统计占用系统内存最多的进程,并排序。
用k
或者--sort
可对指定参数进行排序,-
表示倒序
[root@centos8 ~ ]#ps axo pid,cmd,%mem k -%mem
PID CMD %MEM
866 /usr/libexec/platform-pytho 4.1
867 /usr/libexec/sssd/sssd_nss 3.9
878 /usr/libexec/platform-pytho 3.4
803 /usr/lib/polkit-1/polkitd - 2.5
873 /usr/sbin/NetworkManager -- 1.8
862 /usr/libexec/sssd/sssd_be - 1.5
[root@centos8 ~ ]#ps axo pid,cmd,%mem --sort -%mem
PID CMD %MEM
866 /usr/libexec/platform-pytho 4.1
867 /usr/libexec/sssd/sssd_nss 3.9
878 /usr/libexec/platform-pytho 3.4
803 /usr/lib/polkit-1/polkitd - 2.5
873 /usr/sbin/NetworkManager -- 1.8
862 /usr/libexec/sssd/sssd_be - 1.5
2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"“success!”",若ping不通则输出"“fail!”"
[root@centos8 scripts ]#cat for_ping_network.sh
#!/bin/bash
NET=192.168.0
for i in {1..254};do
{ if `ping -c1 -w1 $NET.$i &> /dev/null` ; then
echo "$NET.$i is success!"
else
echo "$NET.$i is fail!"
fi
}&
done
wait
[root@centos8 scripts ]#cat for_ping_network1.sh
#!/bin/bash
**
NET=192.168.0
for HOST in {1..254};do
{
ping -c1 -w1 $NET.$HOST &> /dev/null&&echo "$NET.$HOST is success!"||echo "$NET.$HOST is fail!"
}&
done
wait
[root@centos8 scripts ]#cat network
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
...
192.168.0.254
[root@centos8 scripts ]#cat while_ping_network.sh
while read IP; do
{
ping -c1 -w1 $IP &> /dev/null && echo "$IP is success!" || echo "$IP is fail!"
}&
done < /data/scripts/network
wait
[root@centos8 scripts ]#cat while_ping_network.sh
#!/bin/bash
#
#**********************************************************************
#Author: awakening
#QQ: 397269789
#Date: 2020-12-14
#Filename: while_ping_network.sh
#URL: http://www.yunmix.top
#Description: The test script
#**********************************************************************
NET=192.168.0
HOST=1
while [ $HOST -lt 255 ]; do
{ ping -c1 -w1 $NET.$HOST &> /dev/null && echo "$NET.$HOST is success!" || echo "$NET.$HOST is fail!"
}&
let HOST++
done
wait
3、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
审题:
1、将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”
.tar.xz
就要用到tar
命令tar Jcvf /backup/etcbak-yyyy-mm-dd-HH.tar.xz /etc/
2、yyyy-mm-dd-HH
并且其中日期是前一天的时间
就要用到date
命令date -d '-1 days' "%F-%H"
3、每周的工作日1:30进行备份,就要用到crontab
计划人物,用crontab -e
进入编辑
[root@centos8 ~ ]#date -d '-1 days' "+%F-%H"
2020-12-13-12
total 0
[root@centos8 ~ ]#tar Jcvf /backup/etcbak-`date -d "-1 days" "+%F-%H"`.tar.xz /etc/
tar: Removing leading '/' from member names
/etc/
/etc/fstab
/etc/crypttab
...
[root@centos8 ~ ]#ll /backup/
total 3152
-rw-r--r--. 1 root root 3226592 Dec 14 12:27 etcbak-2020-12-13-12.tar.xz
[root@centos8 scripts ]#crontab -l
3 12 * * 1-5 /usr/bin/tar Jcvf /backup/etcbak-`date -d "-1 days" "+%F-%H"`.tar.xz /etc/
/*%在cron中有特殊含义,需转译*/
[root@centos8 scripts ]#crontab -l
30 1 * * 1-5 /usr/bin/tar Jcvf /backup/etcbak-`date -d "-1 days" "+\%F-\%H"`.tar.xz /etc
4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警"
脚本文件添加执行权限
[root@centos8 ~ ]#cat /data/scripts/disk_warning.sh
#!/bin/bash
WARNING=80
df | sed -nr '/^\/dev\/sd/s@^([^ ]+).* ([0-9]+)%.*@\1 \2@p' | while read DEVICE USE;do
[ $USE -gt $WARNING ] && echo "$DEVICE is full,use is $USE" | mail -s diskfull root
done
[root@centos8 ~ ]#crontab -l
15 14 * * 1-5 /usr/bin/tar Jcvf /backup/etcbak-`date -d "-1 days" "+\%F-\%H"`.tar.xz /etc/
*/10 * * * 1-5 /data/scripts/disk_warning.s
[root@centos8 scripts ]#cat disk_warning_awk.sh
#!/bin/bash
df | awk -F ' +|%' '/^\/dev\/sd/{if( $5 > 80 ){system("echo "$1" will be full,use:" $5 "| mail -s warning root")} }'
You have mail in /var/spool/mail/root
[root@centos8 scripts ]#crontab -l
15 14 * * 1-5 /usr/bin/tar Jcvf /backup/etcbak-`date -d "-1 days" "+\%F-\%H"`.tar.xz /etc/
*/10 * * * 1-5 /data/scripts/disk_warning.sh
*/10 * * * 1-5 /data/scripts/disk_warning_awk.sh