脚本应用思路
1. 确定命令操作(设计并执行任务)
2. 编写Shell脚本(组织任务过程)
3. 设置计划任务(控制时间,调用任务脚本)
---------------------------------------------
实战-1、实现管理员在登录系统时显示如下信息
1)当前运行的进程数量
2)当前系统的登录用户的数量
3)当前使用的磁盘根分区的使用情况
操作过程如下图:
注意:也可修改/etc/profile配置文件,实现开机运行
脚本源码如下:
#!/bin/bash
# welcome information for login
#
# Print below information:
# 1. The total number of processes.
# 2. The totla number of users who logged in.
# 3. The used status of disk.
echo "########## Welcome! ##########"
# 1. The total number of processes.
echo "The total number of processes is $(ps -aux | wc -l)."
# 2. The totla number of users who logged in.
echo "The totla number of users who logged in is $(who | wc -l)."
# 3. The used status of disk.
echo "The used status of disk is $(df -hT | grep root | awk '{print $6}')."
---------------------------------------------
实战-2、编写脚本实现系统服务启动 停止 当前状态的脚本
#!/bin/bash
#service manager script
# status start stop restart
if [ $1 = 'start' ] #start server of $2
then systemctl start $2 2>>server.logs
elif [ $1 = 'stop' ] #stop server of $2
then systemctl stop $2 2>>server.logs
elif [ $1 = 'status' ] #status server of $2
then systemctl status $2 2>>server.logs
elif [ $1 = 'restart' ] #restart server of $2
then systemctl restart $2 2>>server.logs
fi
---------------------------------------------
实战-3、写运行状况监控脚本
/sh/monitor.sh,用于记录CPU负载、内存和交换空间、磁盘空间、最近的用户登录情况等信息,以及当时的时间信息。
#!/bin/bash
#back monitor CPU Mem Swap Disk LastLogin information to file
# set recored Dir
Dir=/var/log/runrec/
# prepare the environment
mkdir -p $Dir
# get current time
RecTime=$(date +"%Y-%m-%d %H:%M")
# set record filename
RecFile=$Dir$RecTime.log
# get CPU
RecLoad=$(uptime)
# get Mem and Swap
RecMem=$(free -m)
# get Disk
RecDisk=$(df -hT)
# get Last
RecLastLogin=$(last -n 20)
# echo information and saved to RecFile
echo "######################################
Cpu Load information:$RecLoad
Memory information:$RecMem
Disk Usage information:$RecDisk
Last Login 20 users record:$RecLastLogin" >> $RecFile
设置定时备份:
---------------------------------------------
实战-4、过滤出本机echo网卡的MAC地址,并赋值给hwaddr
[root@xiaogan121 sh]# ifconfig | grep ether | awk '{ print $2 }'
00:0c:29:af:03:b2
00:0c:29:af:03:bc
52:54:00:83:20:8c
[root@xiaogan121 sh]# ip=$(ifconfig | grep ether | awk '{ print $2 }')
[root@xiaogan121 sh]# echo $ip
00:0c:29:af:03:b2 00:0c:29:af:03:bc 52:54:00:83:20:8c
[root@xiaogan121 sh]#
---------------------------------------------
实战-5、编写脚本计算当前的内存使用百分比
MemTotal=$(free -m | grep Mem | awk '{ print $2 }')
MemUse=$(free -m | grep Mem | awk '{ print $3 }')
usage=$( expr $( expr $MemUse \* 100 ) / $MemTotal )
[root@xiaogan121 sh]# usage=$( expr $( expr $(free -m | grep Mem | awk '{ print $3 }') \* 100 ) / $(free -m | grep Mem | awk '{ print $2 }') )
[root@xiaogan121 sh]# echo $usage
31
[root@xiaogan121 sh]# free -m | grep Mem | awk '{ print $3 }'
311
[root@xiaogan121 sh]# free -m | grep Mem | awk '{ print $2 }'
977
[root@xiaogan121 sh]# free -m
total used free shared buff/cache available
Mem: 977 312 292 7 373 474
Swap: 2047 0 2047
[root@xiaogan121 sh]#
---------------------------------------------
实战-6、计算3 4 的平方和