1 显示统计占用系统内存最多的进程,并排序。
[root@localhost ~]# ps aux | sort -k4nr | head
mysql 1916 0.0 8.6 968916 86548 ? Sl Aug01 0:20 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 918 0.0 1.7 574300 17468 ? Ssl Aug01 0:05 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root 927 0.0 1.5 1004320 15824 ? Ssl Aug01 0:00 /usr/sbin/libvirtd
polkitd 569 0.0 1.4 614436 14356 ? Ssl Aug01 0:00 /usr/lib/polkit-1/polkitd --no-debug
root 1543 0.0 0.6 163620 6136 ? Ss Aug01 0:00 sshd: root@pts/0
root 560 0.0 0.6 99696 6080 ? Ss Aug01 0:00 /usr/bin/VGAuthService -s
root 574 0.0 0.6 324596 6780 ? Ssl Aug01 0:41 /usr/bin/vmtoolsd
root 60566 0.0 0.6 163620 6140 ? Ss 00:47 0:00 sshd: root@pts/1
root 575 0.0 0.5 228180 5652 ? Ss Aug01 0:00 /usr/sbin/abrtd -d -s
root 615 0.0 0.5 430616 5496 ? Ssl Aug01 0:00 /usr/sbin/ModemManager
2 编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出“success!”,若ping不通则输出“fail!”。
[root@localhost ~]# vim ping_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. success!"
else
echo "$NETID$HOSTID is down. fail!"
fi
} &
done
wait
[root@localhost ~]# ./ping_for.sh
192.168.0.66 is down. fail!
192.168.0.69 is down. fail!
192.168.0.68 is down. fail!
...
[root@localhost ~]# vim ping_while.sh
#!/bin/bash
NETID=192.168.0.
HOSTID=1
while [ $HOSTID -lt 255 ];do
{
if /bin/ping -c1 -W1 $NETID$HOSTID > /dev/null; then
echo "$NETID$HOSTID is up. success!"
else
echo "$NETID$HOSTID is down. fail!"
fi
let HOSTID=${HOSTID}+1
}
done
[root@localhost ~]# ./ping_while.sh
192.168.0.1 is down. fail!
192.168.0.2 is down. fail!
192.168.0.3 is down. fail!
...
3 每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间。
[root@localhost ~]# vim backup.sh
#!/bin/bash
[ -d /backup ] || mkdir /backup
DATE=`date -d yesterday "+%Y-%m-%d-%H"`
tar -Jcf /backup/etcbak-$DATE.tar.xz /etc &> /dev/null
[root@localhost ~]# chmod a+x backup.sh
[root@localhost ~]# vim /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 root /root/backup.sh
4 工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就发送邮件报警。
[root@localhost ~]# vim disk_check.sh
#!/bin/bash
MAX_USAGE_RATE=`df -h | sed -nr '/sda/s/^.* ([0-9]+)%.*/\1/p' | sort -nr | head -1`
MAX_PARTITION_NAME=`df -h | grep $MAX_USAGE_RATE% | cut -d" " -f1`
if [ $MAX_USAGE_RATE -ge 80 ]; then
echo "Warning!$MAX_PARTITION_NAME used $MAX_USAGE_RATE% space!" | mail -s Warning root
fi
[root@localhost ~]# chmod a+x disk_check.sh
[root@localhost ~]# vim /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
*/10 * * * 1-5 root /root/disk_check.sh &> /dev/null