2017-08-28 68 views
0

我想要一個bash腳本,控制cron每天都跑,我想grep從python(Django)輸出的一些行併發布它與slacktee我的閒暇頻道。但我只是從腳本中捕捉一些警告,而不是我自己的打印(與std :: out和std :: err有關)?但我似乎無法調試它。Cronned Django命令輸出沒有張貼在三通從bash腳本

#!/bin/bash 

printf "\nStarting the products update command\n" 
DATE=`date +%Y-%m-%d` 
source mypath/bin/activate 

cd some/path/_production/_server 

./manage.py nn_products_update > logs/product_updates.log 

tail --lines=1000 logs/product_updates.log | grep INFO | grep $DATE 

所以每一天,我試圖給grep這樣的消息:

[INFO][NEURAL_RECO_UPDATE][2017-08-28 22:15:04] Products update... 

但它不會印在T形通道。此外,該文件每天都會被覆蓋,而不是附加 - 請問如何更改該文件?當在shell中自行運行時,tail命令可以正常工作。這怎麼可能? (抱歉問兩個,但我相信他們是某種相關的,只是不能找到答案)

這是萬一cron條目。

20 20 * * * /bin/bash /path/to/server/_production/bin/runReco.sh 2>&1 | slacktee.sh -c 'my_watch' 

非常感謝

編輯:

輸出使用grep -e INFO -e $DATE

grep: [INFO][NEURAL_RECO_UPDATE][2017-08-29: No such file or directory 
grep: 07:36:56]: No such file or directory 
grep: No: No such file or directory 
grep: new: No such file or directory 
grep: active: No such file or directory 
grep: products: No such file or directory 
grep: to: No such file or directory 
grep: calculate.: No such file or directory 
grep: Terminating...: No such file or directory 

回答

0

使用時:

#!/bin/bash 
set -euo pipefile 

這將提供更好的腳本調試輸出 有關設置的完整說明,請參見bash strict mode article

該文件被覆蓋,因爲您使用的是單個>(重定向)而不是>>,其中附加了重定向輸出。

爲了進一步調試,它可能會使生活更容易,如果你把

2> & 1 | slacktee.sh -c 'my_watch'

在你runReco.sh爲:

tail --lines=1000 logs/product_updates.log | grep INFO | grep $DATE 2>&1 | slacktee.sh -c 'my_watch' 

雖然在shell腳本鏈很多命令一起使得它們更難調試。

因此打破了尾行:

TMPFILE=`tail --lines=1000 logs/product_updates.log` 
grep -e INFO -e $DATE $TMPFILE 2>&1 | slacktee.sh -c 'my_watch' 
+0

嗨!感謝你的回答。它有點作品,但是當我按照你的建議使用TMPFILE和grep -e時,如果打印出像我在上面的問題中編輯過的東西。大多數情況下,問題出現在'>'vs'>>'中,現在它可以工作,但'DATE'的grep不起作用,整個帶INFO文件被打印出來。 :( –

+0

確實:'grep -e INFO -e $ DATE logs/product_updates.log'生成正確的輸出?'$ TMPFILE'的輸出可能需要grep的'--line-buffered'選項 – fcbsd

+0

它現在只需用'>>'修復就行了!非常感謝! –