![](/img/trans.png)
[英]Allow commands to run after backgrounding django runserver command in a bash script?
[英]Cronned Django command output not posted in tee from bash script
我正在嘗試讓cron
控制的bash腳本每天運行,我想從python
(Django)輸出中復制一些行並將其與slacktee一起發布到我的slack頻道。 但是我只是從腳本中捕獲了一些警告,而不是我自己的打印內容(與std :: out和std :: err有關)? 但是我似乎無法調試它。
#!/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
使用方法:
#!/bin/bash
set -euo pipefile
這樣可以從腳本中提供更好的調試輸出。有關設置的完整說明,請參見bash嚴格模式文章 。
該文件被覆蓋,因為您使用的是單個>(重定向)而不是>>,該追加附加了重定向的輸出。
為了進一步調試,如果您將
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'
盡管在外殼程序腳本中將許多命令鏈接在一起使它們更難調試。
因此打破了尾線:
TMPFILE=`tail --lines=1000 logs/product_updates.log`
grep -e INFO -e $DATE $TMPFILE 2>&1 | slacktee.sh -c 'my_watch'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.