2013-03-27 77 views
9

我在「logrotate.d」目錄中添加了兩個腳本,以便旋轉我的應用程序日誌。 這是其中的一種配置:logrotate cron作業不旋轉某些日誌

<myLogFilePath> { 
    compress 
    copytruncate 
    delaycompress 
    dateext 
    missingok 
    notifempty 
    daily 
    rotate 30 
} 

有一個「logrotate的」腳本「cron.daily」目錄(這似乎是運行日常按照cron的日誌):

#!/bin/sh 

echo "logrotate_test" >>/tmp/logrotate_test 
#/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1 
/usr/sbin/logrotate -v /etc/logrotate.conf &>>/root/logrotate_error 

EXITVALUE=$? 
if [ $EXITVALUE != 0 ]; then 
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" 
fi 
exit 0 

第一個echo語句正在工作。
但我發現我的應用程序單獨的日誌沒有得到旋轉,而其他日誌像httpd越來越旋轉**
**而且我也沒有看到在提到「logrotate_error」文件
任何輸出(有寫權限爲所有用戶)。

但是系統日誌說:「的logrotate:警惕以異常退出[1]」

但是當我在運行相同的「日誌輪播」,「cron.daily」腳本手動,一切似乎都工作正常。

爲什麼在日常計劃中不旋轉?我在這裏做錯了什麼?
如果我得到這個非常需要的幫助,那將會很棒。

更新: 看起來,這是因爲的SELinux的 - 在我的用戶主目錄中的日誌文件已經限制SELinux所強加和運行logrotate的腳本時:

SELinux is preventing /usr/sbin/logrotate from getattr access on the file /home/user/logs/application.log 

回答

10

的SELinux被限制在不具有所需SELinux文件上下文類型的目錄中的日誌文件上訪問logrotate。 「/ var/log」目錄中有「var_log_t」文件上下文,而logrotate能夠做到這一點。所以解決辦法是將其設置在我的應用程序日誌文件和它的父目錄中:

semanage fcontext -a -t var_log_t <directory/logfile> 
restorecon -v <directory/logfile> 
5

我有類似的問題。要解決這個問題,我首先檢查了SELinux的使用sestatus命令的狀態:

# sestatus 
SELinux status:     enabled 
SELinuxfs mount:    /selinux 
Current mode:     enforcing 
Mode from config file:   enforcing 
Policy version:     24 
Policy from config file:  targeted 

然後,檢查適用於使用LS --scontext文件和目錄的SELinux安全環境。檢查要logrotate的進行操作,並檢查在工作,例如/ var /日誌/ maillog中文件的文件:

# ls --scontext /var/log/maillog* 
system_u:object_r:var_log_t:s0 /var/log/maillog 
system_u:object_r:var_log_t:s0 /var/log/maillog-20140713 
system_u:object_r:var_log_t:s0 /var/log/maillog-20140720 
system_u:object_r:var_log_t:s0 /var/log/maillog-20140727 
system_u:object_r:var_log_t:s0 /var/log/maillog-20140803 

使用semanage的更改文件內容。

semanage fcontext -a -t var_log_t <directory/logfile> 
restorecon -v <directory/logfile> 
2

只是概括以上,並確保同樣的SELinux正確以後所有文件設置:

semanage fcontext -a -t var_log_t "<directory>(/.*)?" 
restorecon -v <directory> 
0

我已經看到了SELINUX這個問題禁用,這是因爲日誌的父目錄旋轉文件具有全局寫許可其不受logrotate的歡迎

error: skipping "/xxx/yyy/log/logfile.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation. 

搭配chmod父目錄,755解決問題

# logrotate --version 
logrotate 3.8.6 
0

SELinux阻止/ usr/sbin/logrotate對目錄站點的讀訪問。

*****插件包羅萬象(100度)表明***************************

如果您相信logrotate默認情況下應該允許在網站目錄上讀取訪問權限。 然後你應該報告這是一個錯誤。 您可以生成本地策略模塊以允許此訪問。
不要
允許這種訪問對於現在執行:

# grep logrotate /var/log/audit/audit.log | audit2allow -M mypol 
# semodule -i mypol.pp 
+1

這哪裏是取自?你可以補充一下嗎?我谷歌搜索了這個,我看到很多類似的消息,但我找不到消息來源。這是SELinux給出的默認消息嗎? – 2017-03-08 20:02:11