歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 使用 Shell 腳本自動化 Linux 系統維護任務

使用 Shell 腳本自動化 Linux 系統維護任務

日期:2017/3/6 9:43:52   编辑:學習Linux

使用 Shell 腳本自動化 Linux 系統維護任務


使用 Shell 腳本自動化 Linux 系統維護任務


shell-automation_01
如果一個系統管理員花費大量的時間解決問題以及做重復的工作,你就應該懷疑他這麼做是否正確。一個高效的系統管理員應該制定一個計劃使得其盡量花費少的時間去做重復的工作。因此盡管看起來他沒有做很多的工作,但那是因為 shell 腳本幫助他完成了大部分任務,這也就是我們將要探討的東西。

什麼是 shell 腳本?

簡單的說,shell 腳本就是一個由 shell 一步一步執行的程序,而 shell 是在 Linux 內核和最終用戶之間提供接口的另一個程序。
默認情況下,RHEL 7 中用戶使用的 shell 是 bash(/bin/bash)。

寫一個腳本顯示系統信息

首先讓我們新建一個目錄用於保存我們的 shell 腳本:

# mkdir scripts# cd scripts

新建一個文本文件system_info.sh,在頭部插入一些注釋以及一些命令:

#!/bin/bash# 該腳本會返回以下這些系統信息:# -主機名稱:echo -e "\e[31;43m***** HOSTNAME INFORMATION *****\e[0m"hostnamectlecho ""# -文件系統磁盤空間使用:echo -e "\e[31;43m***** FILE SYSTEM DISK SPACE USAGE *****\e[0m"df -hecho ""# -系統空閒和使用中的內存:echo -e "\e[31;43m ***** FREE AND USED MEMORY *****\e[0m"freeecho ""# -系統啟動時間:echo -e "\e[31;43m***** SYSTEM UPTIME AND LOAD *****\e[0m"uptimeecho ""# -登錄的用戶:echo -e "\e[31;43m***** CURRENTLY LOGGED-IN USERS *****\e[0m"whoecho ""# -使用內存最多的 5 個進程echo -e "\e[31;43m***** TOP 5 MEMORY-CONSUMING PROCESSES *****\e[0m"ps -eo %mem,%cpu,comm --sort=-%mem | head -n 6echo ""echo -e "\e[1;32mDone.\e[0m"

然後,給腳本可執行權限,並運行腳本:

# chmod +x system_info.sh./system_info.sh

為了更好的可視化效果各部分標題都用顏色顯示:
shell-automation_02
顏色功能是由以下命令提供的:

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"updatedbif [ $? == 0 ]; then        echo "The local file database was updated correctly."else        echo "The local file database was not updated correctly."fiecho ""# -查找 和/或 刪除有 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=30while 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"        fidone < <(df -h --total | grep -vi filesystem)

請注意該腳本最後一行兩個 < 符號之間有個空格。
shell-automation_03

使用 Cron

想更進一步提高效率,你不會只是想坐在你的電腦前手動執行這些腳本。相反,你會使用 cron 來調度這些任務周期性地執行,並把結果通過郵件發送給預先指定的接收者,或者將它們保存到使用 web 浏覽器可以查看的文件中。
下面的腳本(filesystem_usage.sh)會運行有名的 df -h 命令,格式化輸出到 HTML 表格並保存到 report.html 文件中:

#!/bin/bash# 演示使用 shell 腳本創建 HTML 報告的示例腳本# Web directoryWEB_DIR=/var/www/html# A little CSS and table layout to make the report look a little nicerecho "<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 bodyHOST=$(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 linewhile read line; doecho "<tr><td align='center'>" >> $WEB_DIR/report.htmlecho $line | awk '{print $1}' >> $WEB_DIR/report.htmlecho "</td><td align='center'>" >> $WEB_DIR/report.htmlecho $line | awk '{print $2}' >> $WEB_DIR/report.htmlecho "</td><td align='center'>" >> $WEB_DIR/report.htmlecho $line | awk '{print $5}' >> $WEB_DIR/report.htmlecho "</td></tr>" >> $WEB_DIR/report.htmldone < <(df -h | grep -vi filesystem) echo "</table></BODY></HTML>" >> $WEB_DIR/report.html

在我們的 RHEL 7 服務器(192.168.0.18)中,看起來像下面這樣:
shell-automation_04
你可以添加任何你想要的信息到那個報告中。添加下面的 crontab 條目在每天下午的 1:30 運行該腳本:

30 13 * * * /root/scripts/filesystem_usage.sh

原文地址:http://www.tecmint.com/using-shell-script-to-automate-linux...作者:Gabriel Cánepa

轉載地址:https://linux.cn/article-6526-1.html譯者:ictlyh

本文轉載自::http://www.linuxprobe.com/shell-automation-maintenance-tasks/

免費提供最新Linux技術教程書籍,為開源技術愛好者努力做得更多更好:http://www.linuxprobe.com/

http://xxxxxx/Linuxjc/1134339.html TechArticle

Copyright © Linux教程網 All Rights Reserved