1、显示占用系统内存最多的进程,并排序
以下几种写法都可以:
ps aux --sort=-%mem
ps aux --sort=-rss
ps aux --sort=-rssize
ps aux --sort=-rsz
ps aux | sort -k4nr
------------------------------------------------------------
[root@centos6 ~]# ps aux --sort -rss |head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 28743 0.0 0.4 98008 4112 ? Ss 08:55 0:03 sshd: root@pts/0,pts/1,pts/2
postfix 41028 0.0 0.3 81092 3440 ? S 18:52 0:00 pickup -l -t fifo -u
root 28747 0.0 0.2 108396 2040 pts/0 Ss 08:55 0:01 -bash
root 28766 0.0 0.1 108320 1948 pts/1 Ss+ 08:56 0:00 -bash
[root@centos6 ~]# ps aux |sort -k4nr |head -5
root 28743 0.0 0.4 98008 4112 ? Ss 08:55 0:03 sshd: root@pts/0,pts/1,pts/2
postfix 41028 0.0 0.3 81092 3440 ? S 18:52 0:00 pickup -l -t fifo -u
root 28747 0.0 0.2 108396 2040 pts/0 Ss 08:55 0:01 -bash
postfix 1301 0.0 0.1 81260 1188 ? S Mar07 0:00 qmgr -l -t fifo -u
root 1302 0.0 0.1 116916 1056 ? Ss Mar07 0:01 crond
2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
for循环:
[root@centos6 data]# vim ip_activeck_for.sh
#!/bin/bash
NETID=192.168.0.
for HOSTID in {1..254};do
{
if /bin/ping -c1 -W1 $NETID$HOSTID >/dev/null ;then
echo "$NETID$HOSTID is up."
else
echo "$NETID$HOSTID is down."
fi
} & ###并发执行
done
wait
[root@centos6 data]# chmod +x ip_activeck_for.sh
[root@centos6 data]# sh ip_activeck_for.sh
192.168.0.5 is down.
192.168.0.6 is down.
192.168.0.7 is down.
192.168.0.8 is down.
192.168.0.9 is down.
...
192.168.0.250 is down.
192.168.0.253 is down.
192.168.0.254 is down.
while循环:
生成要扫描的IP列表:
[root@centos6 data]# echo 192.168.0.{1..254} |tr -s " " "\n" > ip.txt
[root@centos6 data]# vim ip_activeck_while.sh
#!/bin/bash
while read ip;do
{
/bin/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@centos6 data]# chmod +x ip_activeck_while.sh
[root@centos6 data]# sh ip_activeck_while.sh
192.168.0.2 is down.
192.168.0.3 is down.
192.168.0.4 is down.
192.168.0.1 is down.
...
192.168.0.248 is down.
192.168.0.251 is down.
192.168.0.250 is down.
3、每周的周日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
创建脚本:etc_bakcup.sh
[root@centos6 data]# vim etc_backup.sh
#!/bin/bash
[ -d /backup ] || mkdir /backup
rpm -q xz >/dev/null
[ $? -eq 0 ] || yum -y install xz >/dev/null
DATEFORMAT=`date -d yesterday "+%Y-%m-%d-%H"`
tar -cf /backup/etcbak-$DATEFORMAT.tar /etc > /dev/null && xz -z /backup/etcbak-$DATEFORMAT.tar
[root@centos6 data]# chmod +x etc_backup.sh
[root@centos6 data]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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 * * 0 root sh /data/etc_backup.sh &>/dev/null
4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警
[root@centos6 data]# vim disk_check.sh
#!/bin/bash
#取分区使用率的最大值
#sed方法
#Usage_Rate=`df |sed -nr "/\/dev\/sda/s#.* +([0-9]+)% .*#\1#p" |sort -nr |head -1`
#awk方法
Usage_Rate=`df |awk -F '[ %]+' 'BEGIN{max = 0}{if ($5+0 > max) max=$5} END {print max}'`
#取使用率最大分区名
Partition_Name=`df |grep "$Usage_Rate%" |cut -d" " -f1`
if [ $Usage_Rate -gt 80 ];then
echo -e "Warning!!! \n $Partition_Name's avaliable space is less than 20%" |mail -s Warning root
fi
[root@centos6 data]# chmod +x disk_check.sh
[root@centos6 data]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
*/10 * * * 1-5 root sh /data/disk_check.sh &>/dev/null
为了测试效果,在/boot分区下创建文件,将/boot分区使用率超过80%
[root@centos6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.5G 866M 8.2G 10% /
tmpfs 490M 0 490M 0% /dev/shm
/dev/sda1 190M 31M 150M 17% /boot
/dev/sda5 7.8G 19M 7.4G 1% /data
[root@centos6 ~]# dd if=/dev/zero of=/boot/bigfile bs=1M count=130
130+0 records in
130+0 records out
136314880 bytes (136 MB) copied, 3.09203 s, 44.1 MB/s
[root@centos6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.5G 866M 8.2G 10% /
tmpfs 490M 0 490M 0% /dev/shm
/dev/sda1 190M 161M 20M 90% /boot
/dev/sda5 7.8G 19M 7.4G 1% /data
更改计划任务的检测时间为1分钟
[root@centos6 ~]# vim /etc/crontab
*/1 * * * 1-5 root sh /data/disk_check.sh &>/dev/null
检查计划任务日志,检查邮件
[root@centos6 ~]# tail /var/log/cron
Mar 9 22:00:01 centos6 crond[42597]: (*system*) RELOAD (/etc/crontab)
Mar 9 22:00:01 centos6 CROND[42689]: (root) CMD (sh /data/disk_check.sh &>/dev/null )
Mar 9 22:01:01 centos6 CROND[42712]: (root) CMD (sh /data/disk_check.sh &>/dev/null )
Mar 9 22:01:01 centos6 CROND[42711]: (root) CMD (run-parts /etc/cron.hourly)
Mar 9 22:01:02 centos6 run-parts(/etc/cron.hourly)[42711]: starting 0anacron
Mar 9 22:01:02 centos6 anacron[42736]: Anacron started on 2020-03-09
Mar 9 22:01:02 centos6 run-parts(/etc/cron.hourly)[42738]: finished 0anacron
Mar 9 22:01:02 centos6 anacron[42736]: Jobs will be executed sequentially
Mar 9 22:01:02 centos6 anacron[42736]: Normal exit (0 jobs run)
Mar 9 22:02:01 centos6 CROND[42743]: (root) CMD (sh /data/disk_check.sh &>/dev/null )
You have new mail in /var/spool/mail/root
[root@centos6 ~]# mail
Heirloom Mail version 12.4 7/29/08. Type ? for help.
"/var/spool/mail/root": 7 messages 3 new 7 unread
U 1 root Mon Mar 9 21:56 20/652 "Warning"
U 2 root Mon Mar 9 21:57 20/652 "Warning"
U 3 root Mon Mar 9 21:58 20/652 "Warning"
U 4 root Mon Mar 9 21:59 20/652 "Warning"
>N 5 root Mon Mar 9 22:00 19/642 "Warning"
N 6 root Mon Mar 9 22:01 19/642 "Warning"
N 7 root Mon Mar 9 22:02 19/642 "Warning"
& 7
Message 7:
From root@centos6.localdomain Mon Mar 9 22:02:01 2020
Return-Path: <root@centos6.localdomain>
X-Original-To: root
Delivered-To: root@centos6.localdomain
Date: Mon, 09 Mar 2020 22:02:01 +0800
To: root@centos6.localdomain
Subject: Warning
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: root@centos6.localdomain (root)
Status: R
Warning!!!
/dev/sda1's avaliable space is less than 20%
确认脚本和计划任务都能正常运行。
本文详细介绍如何在Linux系统中监控内存使用情况,通过编写Shell脚本实现网络连通性检查,设置定时备份任务,以及自动化磁盘空间预警机制。通过具体示例,展示如何利用ps、ping、crontab等工具进行系统管理和维护。

1427

被折叠的 条评论
为什么被折叠?



