我編寫了這個腳本來自動處理來自不同服務器的wgets,並將結果輸出到日誌文件,並通過電子郵件發送給我。我分享這一部分原因是爲了讓其他人可以爲自己使用解決方案。使用文件中的行作爲變量,提供給程序 - 自動執行wget
我把文件放在cron.weekly中,所以它每週給我發電子郵件。
Btw。我是一個絕對的新手,這是我通過從這個網站學習而寫的第一個腳本。
腳本如下: 我也從我這邊做了一些小的評論。
#!/bin/sh
#wget -verbose -tries -timeout -dontcreatedirectories -dontsaveanything http://x output text to file
# automating apt-get update and upgrade so it runs weekly
apt-get update
apt-get upgrade -y
# this is where the logs will be stored
cd /var/log
# create a tmp file and if it is left behind from earlier results, empty it out
echo -e "\n" > wget.tmp
# these are the header texts for the results
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "WGET SCHEDULES" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
# this will make note of the time the tests are started
date "+START OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
# up to 10 servers to wget from
echo "Server #1" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
wget -v -r -t 2 -T 7 -nd -O /dev/null http://servername/file 2>> wget.tmp
grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
rm -f wget.tmp
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
。 繼續到 。
echo "Server #10" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
wget -v -r -t 2 -T 7 -nd -O /dev/null http://servername/file 2>> wget.tmp
grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
rm -f wget.tmp
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
# record the end time of tests
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
date "+END OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
#mail results to yourself
echo "Subject: $(hostname -s| tr a-z A-Z)" Wget Results v.2.0 Sendmail "$(date +%x)" ---- "$(date "+%A")" "$(date "+%X") ("$(date "+%Z")") ::: Week "$(date "+%V")" """ | cat - $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log | /usr/lib/sendmail -t [email protected]
理想情況下,我想不限制自己的服務器X#,但只要有文本文件servers.list(包括由「」在文件名中,我可以將其存儲在cron。每週一次,不會被處理)。因此,對於每個服務器,寫入服務器#,並處理wget,輸出到文件,grep以記錄文件。這將是最好的,如果servers.list文件格式化
hostname filename store/to
.
.
hostname filename store/to
例子:
msn.robots.ua 100mb.test /dev/null
justme.cov ratings.xlsx /srv/storage/latest
的郵件看起來像
主題:服務器Wget的結果2.0版的Sendmail 12年9月9日----週日四點59分21秒(MSK):::第36周
WGET時間表
連接80 ...:|在Sun公司9月9日四點52分00秒2012服務器#1 WGETS附表第
開始連接到xxx.xxxxxx.com | ip.ip.ip.ip 。 Length:104857600(100M)[text/plain] 102350K .......... .......... .......... ...... .... .......... 100%5.73M 0s 102400K 100%0.00 = 25s 2012-09-09 04:52:27(4.07 MB/s) - `/ dev/null'保存[104857600/104857600] 。 。在Sun公司9月9日4時59分21秒2012
WGETS附表第
END所以我的/ var/log中結束存儲文件,如WordPress,wget的 - 概要 - sep24_2012.log作爲示例文件名稱。問題在於它正在成爲一個問題,服務器發生了變化,所以我想只更新一個位置的服務器列表,讓他們自己處理它。在執行之前,它會下載新的servers.list,其餘的完成。
郵件正在工作輸出到日誌文件正在工作,它都在工作,只需要一個微小的位調整。
Ty! Ty! Ty爲閱讀。
優秀!它的工作原理,我做了一些閱讀後增加了一點點增量:)非常感謝!出於某種原因,我不能做+1,因爲我的代表如此之低。但你的建議做到了。 – Faruq
a +1給你的答案.. = \ – Faruq