使用 Shell 脚本自动化 Linux 系统维护任务_shell实现管理任务

echo -e “\e[1;32mDone.\e[0m”


 然后,给脚本可执行权限,并运行脚本:



chmod +x system_info.sh

./system_info.sh


 为了更好的可视化效果各部分标题都用颜色显示:  
 ![shell-automation_02](http://www.linuxprobe.com/wp-content/uploads/2016/04/shell-automation_02.png)  
 颜色功能是由以下命令提供的:



echo -e “\e[COLOR1;COLOR2m\e[0m”


 其中 COLOR1 和 COLOR2 是前景色和背景色,是你想用颜色显示的字符串。



使任务自动化

 你想使其自动化的任务可能因情况而不同。因此,我们不可能在一篇文章中覆盖所有可能的场景,但是我们会介绍使用 shell 脚本可以使其自动化的三种典型任务:  
 1) 更新本地文件数据库  
 1) 查找(或者删除)有 777 权限的文件  
 2) 文件系统使用超过定义的阀值时发出警告。  
 让我们在脚本目录中新建一个名为 auto\_tasks.sh 的文件并添加以下内容:



#!/bin/bash

自动化任务示例脚本:

-更新本地文件数据库:

echo -e “\e[4;32mUPDATING LOCAL FILE DATABASE\e[0m”
updatedb
if [ $? == 0 ]; then
echo “The local file database was updated correctly.”
else
echo “The local file database was not updated correctly.”
fi
echo “”

-查找 和/或 删除有 777 权限的文件。

echo -e “\e[4;32mLOOKING FOR FILES WITH 777 PERMISSIONS\e[0m”

Enable either option (comment out the other line), but not both.

Option 1: Delete files without prompting for confirmation. Assumes GNU version of find.

#find -type f -perm 0777 -delete

Option 2: Ask for confirmation before deleting files. More portable across systems.

find -type f -perm 0777 -exec rm -i {} +;
echo “”

-文件系统使用率超过定义的阀值时发出警告

echo -e “\e[4;32mCHECKING FILE SYSTEM USAGE\e[0m”
THRESHOLD=30
while read line; do
# This variable stores the file system path as a string
FILESYSTEM=$(echo $line | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲') # Th…(echo $line | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 5}̲') # Us…{PERCENTAGE%?}
if [ $USAGE -gt $THRESHOLD ]; then
echo “The remaining available space in $FILESYSTEM is critically low. Used: $PERCENTAGE”
fi
done < <(df -h --total | grep -vi filesystem)


 请注意该脚本最后一行两个 < 符号之间有个空格。  
 ![shell-automation_03](http://www.linuxprobe.com/wp-content/uploads/2016/04/shell-automation_03.png)



使用 Cron

 想更进一步提高效率,你不会只是想坐在你的电脑前手动执行这些脚本。相反,你会使用 cron 来调度这些任务周期性地执行,并把结果通过邮件发送给预先指定的接收者,或者将它们保存到使用 web 浏览器可以查看的文件中。  
 下面的脚本(filesystem\_usage.sh)会运行有名的 df -h 命令,格式化输出到 HTML 表格并保存到 report.html 文件中:



#!/bin/bash

演示使用 shell 脚本创建 HTML 报告的示例脚本

Web directory

WEB_DIR=/var/www/html

A little CSS and table layout to make the report look a little nicer

echo "

" > $WEB_DIR/report.html # View hostname and insert it at the top of the html body HOST=$(hostname) echo "Filesystem usage for host $HOST
Last updated: $(date)

" >> $WEB_DIR/report.html # Read the output of df -h line by line while read line; do echo "" >> $WEB_DIR/report.html done < <(df -h | grep -vi filesystem) echo "
Filesystem Size Use %
" >> $WEB_DIR/report.html echo $line | awk '{print $1}' >> $WEB_DIR/report.html echo "" >> $WEB_DIR/report.html echo $line | awk '{print $2}' >> $WEB_DIR/report.html echo "" >> $WEB_DIR/report.html echo $line | awk '{print $5}' >> $WEB_DIR/report.html echo "
" >> $WEB_DIR/report.html ```

在我们的 RHEL 7 服务器(192.168.0.18)中,看起来像下面这样:
shell-automation_04

img
img
img

67)]

[外链图片转存中…(img-l1oRsAYd-1725964366768)]
[外链图片转存中…(img-FoH3vJc3-1725964366768)]
[外链图片转存中…(img-tOeYZJS3-1725964366769)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值