之前我聽說高效的系統管理者的一個特點是懶惰。一開始看起來很沖突,但作者接下來解釋了其中的原因:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu4Gb0FnYpl2c3tmeuNXc0ZXMwATNyIzLcRDMvwVMxUTMwIzLc1WdixWYvwFduVWboNWY0RXYvwVY0FGZvwVZt5CevJWcu42Y4VnbpxWLuR2Lc9CX6MHc0RHaiojIsJye.png)
rhce 系列:第四部分 - 自動化 linux 系統維護任務
<a target="_blank"></a>
簡單的說,shell 腳本就是一個由 shell 一步一步執行的程式,而 shell 是在 linux 核心和最終使用者之間提供接口的另一個程式。
為了友善,首先讓我們建立一個目錄用于儲存我們的 shell 腳本:
# mkdir scripts
# cd scripts
然後用喜歡的文本編輯器打開新的文本檔案 system_info.sh。我們首先在頭部插入一些注釋以及一些指令:
#!/bin/bash
# rhce 系列第四部分示例腳本
# 該腳本會傳回以下這些系統資訊:
# -主機名稱:
echo -e "\e[31;43m***** hostname information *****\e[0m"
hostnamectl
echo ""
# -檔案系統磁盤空間使用:
echo -e "\e[31;43m***** file system disk space usage *****\e[0m"
df -h
# -系統空閑和使用中的記憶體:
echo -e "\e[31;43m ***** free and used memory *****\e[0m"
free
# -系統啟動時間:
echo -e "\e[31;43m***** system uptime and load *****\e[0m"
uptime
# -登入的使用者:
echo -e "\e[31;43m***** currently logged-in users *****\e[0m"
who
# -使用記憶體最多的 5 個程序
echo -e "\e[31;43m***** top 5 memory-consuming processes *****\e[0m"
ps -eo %mem,%cpu,comm --sort=-%mem | head -n 6
echo -e "\e[1;32mdone.\e[0m"
然後,給腳本可執行權限:
# chmod +x system_info.sh
運作腳本:
./system_info.sh
注意為了更好的可視化效果各部分标題都用顔色顯示:
伺服器監視 shell 腳本
顔色功能是由以下指令提供的:
echo -e "\e[color1;color2m<your text here>\e[0m"
你想使其自動化的任務可能因情況而不同。是以,我們不可能在一篇文章中覆寫所有可能的場景,但是我們會介紹使用 shell 腳本可以使其自動化的三種典型任務:
1) 更新本地檔案資料庫, 2) 查找(或者删除)有 777 權限的檔案, 以及 3) 檔案系統使用超過定義的閥值時發出警告。
讓我們在腳本目錄中建立一個名為 auto_tasks.sh 的檔案并添加以下内容:
# 自動化任務示例腳本:
# -更新本地檔案資料庫:
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
# -查找 和/或 删除有 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 -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 $1}')
# this variable stores the use percentage (xx%)
percentage=$(echo $line | awk '{print $5}')
# use percentage without the % sign.
usage=${percentage%?}
if [ $usage -gt $threshold ]; then
echo "the remaining available space in $filesystem is critically low. used: $percentage"
done < <(df -h --total | grep -vi filesystem)
請注意該腳本最後一行兩個 < 符号之間有個空格。
查找 777 權限檔案的 shell 腳本
想更進一步提高效率,你不會想隻是坐在你的電腦前手動執行這些腳本。相反,你會使用 cron 來排程這些任務周期性地執行,并把結果通過郵件發動給預先指定的接收者,或者将它們儲存到使用 web 浏覽器可以檢視的檔案中。
下面的腳本(filesystem_usage.sh)會運作有名的 df -h 指令,格式化輸出到 html 表格并儲存到 report.html檔案中:
# 示範使用 shell 腳本建立 html 報告的示例腳本
# web directory
web_dir=/var/www/html
# a little css and table layout to make the report look a little nicer
echo "<html>
<head>
<style>
.titulo{font-size: 1em; color: white; background:#0863ce; padding: 0.1em 0.2em;}
table
{
border-collapse:collapse;
}
table, td, th
border:1px solid black;
</style>
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
</head>
<body>" > $web_dir/report.html
# view hostname and insert it at the top of the html body
host=$(hostname)
echo "filesystem usage for host <strong>$host</strong><br>
last updated: <strong>$(date)</strong><br><br>
<table border='1'>
<tr><th class='titulo'>filesystem</td>
<th class='titulo'>size</td>
<th class='titulo'>use %</td>
</tr>" >> $web_dir/report.html
# read the output of df -h line by line
echo "<tr><td align='center'>" >> $web_dir/report.html
echo $line | awk '{print $1}' >> $web_dir/report.html
echo "</td><td align='center'>" >> $web_dir/report.html
echo $line | awk '{print $2}' >> $web_dir/report.html
echo $line | awk '{print $5}' >> $web_dir/report.html
echo "</td></tr>" >> $web_dir/report.html
done < <(df -h | grep -vi filesystem)
echo "</table></body></html>" >> $web_dir/report.html
在我們的 rhel 7 伺服器(192.168.0.18)中,看起來像下面這樣:
伺服器監視報告
你可以添加任何你想要的資訊到那個報告中。添加下面的 crontab 條目在每天下午的 1:30 運作該腳本:
30 13 * * * /root/scripts/filesystem_usage.sh
你很可能想起各種其他想要自動化的任務;正如你看到的,使用 shell 腳本能極大的簡化任務。
本文來自雲栖社群合作夥伴“linux中國”,原文釋出日期:2015-11-05