1. 程式人生 > >Linux crontab 輸出重定向不生效問題解決

Linux crontab 輸出重定向不生效問題解決

本部落格所有文章採用的授權方式為 自由轉載-非商用-非衍生-保持署名 ,轉載請務必註明出處,謝謝。

宣告:
本部落格歡迎轉發,但請註明出處,保留原作者資訊
部落格地址:孟阿龍的部落格
所有內容為本人學習、研究、總結。如有雷同,實屬榮幸


問題

近期在crontab中添加了一個定時任務,該任務執行之後預設會有正常輸出。為了確保在任務執行過程中的異常資訊也可以捕獲,方便問題定位,因此在crontab中我寫了這麼一條命令:

01 09 * * * cd /opdir/test/ && ./test.sh &>>test.log

以上命令非常好理解,每天9:01執行test.sh 指令碼並且將指令碼的標準錯誤輸出、標準輸出全部重定向到檔案 test.log中。最終發現指令碼是正常執行了,但是test.log 這個日誌檔案中卻沒有任何內容。

為了解決和解釋這個問題,接下來我們先簡單介紹下linux系統中重定向的問題

概念

Linux系統中:
1: 表示標準輸出(stdout),預設輸出到螢幕
2:表示標準錯誤輸出(stderr),預設輸出到螢幕

在平時我們經常使用如下方法將指令碼執行結果重定向:

bash test.sh >test.out     //指令碼的標準輸出寫入到檔案test.out ,標準錯誤輸出直接列印在螢幕 等價於:bash test.sh 1>test.out
bash test.sh >test.out 2>&1 //標準輸出和標準錯誤輸出都寫入到test.out並且不會互相覆蓋,等價於 bash test.sh &>test.out
bash test.sh >test.out 2>test.out //標準輸出和標準錯誤輸出都寫入到test.out,會出現互相覆蓋的問題,正常情況不推薦這樣使用
bash test.sh &>test.out //等價於第二種方法

比較一下以上幾種的效果:

  1. 第一種:錯誤輸出在螢幕,正常輸出在檔案test.out
[email protected]:~/opdir/mengalong/t/t# cat test.sh
#!/bin/bash
t
date

[email protected]:~/opdir/mengalong/t/t# bash test.sh >test.out
test.sh: line 2: t: command not found
[email protected]:~/opdir/mengalong/t/t# cat test.out
Wed Oct 31 11:07:24 CST 2018
  1. 第二種:錯誤輸出和正常輸出均重定向到檔案test.out中
[email protected]:~/opdir/mengalong/t/t# bash test.sh >test.out 2>&1
[email protected]:~/opdir/mengalong/t/t# cat test.out
test.sh: line 2: t: command not found
Wed Oct 31 11:09:02 CST 2018
  1. 第三種:錯誤輸出和正常輸出互相覆蓋
[email protected]:~/opdir/mengalong/t/t# bash test.sh >test.out 2>test.out
[email protected]:~/opdir/mengalong/t/t# cat test.out
Wed Oct 31 11:10:36 CST 2018
ot found
  1. 第四種,特殊情況,比較一下bash test.sh 2>&1 >test.out 和 bash test.sh >test.out 2>&1 的區別:
[email protected]:~/opdir/mengalong/t/t# bash test.sh 2>&1  >test.out
test.sh: line 2: t: command not found
[email protected]:~/opdir/mengalong/t/t# cat test.out
Wed Oct 31 11:12:13 CST 2018

這裡只是把 2>&1 放在了 >test.out 前邊,但是結果卻不是像我們想象的那樣,錯誤和正常輸出都進入test.out 檔案。這是因為, bash test.sh 2>&1 >test.out 這個命令中, 2>&1 的時候,只是把錯誤輸出重定向到了標準輸出,而此時標準輸出的預設值是螢幕,因此實際等價於標準錯誤輸出被重定向到了螢幕,而非檔案。因此重定向需要注意順序。

問題解決

接下來再回過頭來看看,我寫的crontab任務:

01 09 * * * cd /opdir/test/ && ./test.sh &>>test.log

按照上邊的概念分析,這種寫法應該等價於./test.sh >test.log 2>&1 ,指令碼執行的輸出和標準錯誤輸出全部重定向到 test.log。但是實際情況卻是test.log檔案中並沒有任何內容。

這是因為 crontab 預設使用的shell環境為 /bin/sh, 而/bin/sh 並不支援 &>>test.log 這種重定向方法,因此我們看到的效果是test.log 中沒有內容。
因此解決問題的方法就是將crontab的重定向方法進行修改:

01 09 * * * cd /opdir/test/ && ./test.sh >>test.log 2>&1

囉嗦一句

crontab執行過程中,如果指令碼輸出沒有重定向,那麼會預設給系統使用者發郵件,郵件內容一般儲存在 /var/mail/$user 中,如果不清理就會打滿伺服器根分割槽,最終導致機器無法登陸。因此推薦的crontab命令寫法如下:

01 09 * * * cd /opdir/test/ && ./test.sh >>test.log 2>&1 </dev/null &

具體後邊增加了 </dev/null & ,這個的含義就不多說了,感興趣的可以自己分析一下