轉 自定義 crontab日誌檔案位置

2021-06-08 12:37:25 字數 1968 閱讀 9391

原址如下:

crontab是linux下最常用的計畫任務服務。本文跟大家分享一下有關linux crontab日誌存放的一些心得,希望本文能教會你更多東西。

預設情況下,crontab中執行的日誌寫在/var/log下(以centos為例進行說明),如:

#ls /var/log/cron*
/var/log/cron /var/log/cron.1 /var/log/cron.2 /var/log/cron.3 /var/log/cron.4
如果日誌有問題,可以參考以下做法:

為crontab增加日誌

crontab的日誌比較簡單,當crond執行任務失敗時會給使用者發一封郵件。恰巧在我們的幾台web伺服器上發現一些問題:

簡單環境說明:

公司有web1  web2  web3  三颱應用伺服器,其中這三颱伺服器通過rsync實現資料實時同步;web1 為rsync客戶端,web2  web3 為rsync服務端,web1上定製rsync同步指令碼通過cron任務實現定時向web2  web3同步資料。在使用過程中發現cron任務能正常執行,但是同步資料沒有成功;使用手動方式一切正常,為什麼??

檢視日誌發現如下顯示:

jul 20 10:47:01 webmaster crond[3193]: (root) cmd (/root/shell/rsync.sh)

而且crond發郵件也失敗了。

如何檢視cron任務是否真正執行?

考慮到是否為crontab中的任務增加自己的日誌,這樣出錯後,比較容易看到原因。

於是我做了如下操作:

crontab   -e           //編輯cron任務,以下為cron任務內容

# rsync aplication from /www

*/1 * * * * /root/shell/rsync.sh >>/root/shell/for_crontab/mycron.log  2>&1

//紅色部分為新增的內容

//把錯誤輸出和標準輸出都輸出到mycron.log中。

通過檢視mycron.log就可以清楚的知道cron任務是否執行正確了!!!

以下是我的mycron.log檔案中顯示的內容

[root@webmaster for_crontab]# head -n 10 mycron.log 

rsync: failed to open exclude file rules.txt: no such file or directory (2)

rsync error: error in file io (code 11) at exclude.c(1000) [client=2.6.8]

從中可以看出是我的rsync指令碼中使用exclude排除檔案中出了錯誤。。。

附:rsync指令碼內容

#!/bin/sh

# rsync aplication from /www

/usr/bin/rsync -vzrtopg --progress --delete --exclude-from=

/root/shell/

rules.txt  --password-file=/etc/rsync/rsyncd.scrt /www/* [email protected]::www

/usr/bin/rsync -vzrtopg --progress --delete --exclude-from=

/root/shell/

rules.txt  --password-file=/etc/rsync/rsyncd.scrt /www/* [email protected]::www ~

紅色標識部分為錯誤根源,因為之前沒有指明exclude檔案的絕對路徑,導致cron任務找不著排除檔案,故不能執行指令碼檔案。

自定義日誌檔案

借用 部落格 usr bin python coding utf 8 importlogging importos.path importtime 自定義日誌類 classlogger object def init self,logger 指定儲存日誌的檔案路徑,日誌級別,以及呼叫檔案 將日誌存入...

自定義日誌記錄檔案

舉例 郵件傳送錯誤記錄日誌 日誌記錄介面 ilogservice.cs public inte ce ilogservice 日誌記錄介面 實現 filelogservice.cs public class filelogservice ilogservice finally catch endre...

storm 自定義 日誌檔案

storm中的日誌級級別預設為info,並且,日誌檔案是根據worker號來進行區分的,這樣,同乙個log檔案中的資訊不一定是乙個業務的,這樣就會有以下兩個需求出現 1.想要進行一些除錯資訊的輸出 2.除錯資訊或者業務日誌資訊想要輸出到一些固定的檔案中 不要怕,不要煩惱,其實storm已經提供了這樣...