NCLP--考前備忘8--log和script
Log and Logrotate
source基本上不用動, 接聽外部log把 <udp(ip("0.0.0.0") port(514));> 打開
注意TCP還是UDP
filter 層級 <debug info notice warning err crit alert emerg> 嚴重性遞增
destination 到檔案<file("路徑");>, 到畫面<file("/dev/ttyXX");>
到遠端<udp/tcp("IP" (PORT));>, 到指定使用者登入畫面<usertty(USER);>
<man syslog-ng.conf syslog>
logrotate參考檔案 </etc/logrotate.d/*>
<man logrotate>
Shell Script
注意script所在目錄權限和檔案權限
注意命令要用絕對路徑
判斷資料是否存在: [ ! -f /xxx/abc.log ] && touch /xxx/abc.log
傳回值$?, 第一個參數$1, 第二個參數$2, 運用
quota報告寄給root
例: repquota -a | tee /var/log/quotareport | mail -s "Quota Report" root
監控網路使用狀況存到檔案
例: top -b -n $1 -u wwwrun > /xxx/xxx/apache-monitor-$(date +%F-%R)
差異備份/增益備份
例: tar -g 清單檔 -czvf 備份檔 來源
增益備份重複做,差異備份要把清單檔複製回來再做
擷取第N個字母 <cut -c N FILE>
擷取以該間隔第N個 <cut -d '間隔物' -f N FILE>
擷取第M和N個字 <awk '{print $M,$N}' FILE>
擷取以間隔物的第M和N個字 <awk -F 間隔物 '{print $M,$N}' FILE>
取得今天的星期 <day=`date +%u`>
其他日期相關 <man date>
錯誤寄信給USER <echo "訊息" | mail USER -s "SUBJECT">
或是<mail USER -s "SUBJECT" < FILE>
以夾帶檔案方式寄信 <uuencode FILE NAME | mail USER -s "SUBJECT">
只保留幾天內的檔案 <find /PATH -atime +DAY -exec rm -rf {} \;>
有很多找特定檔案可以用到find <man find>
判斷式 <man test>, 各種語法<man bash>
測試script <bash -x SCRIPT>
會搭配cron排程, 編輯<crontab -e>, 檢視<crontab -l>
格式: 分 時 日 月 週 命令
測試時先用 <* * * * * COMMAND> 測試, 不然怎麼死的都不知道
<man 5 crontab>注意要加5