【问题标题】:Cronned Django command output not posted in tee from bash scriptCronned Django 命令输出未从 bash 脚本发布到 tee
【发布时间】:2018-02-06 06:22:57
【问题描述】:

我正在尝试每天运行一个由 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...

但它不会在 tee 通道中打印出来。此外,该文件每天都会被覆盖而不是附加 - 请问如何更改它? tail 命令在 shell 中单独运行时可以正常工作。这怎么可能? (抱歉问了两个,但我相信它们有某种相关性,只是找不到答案)

这是以防万一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

【问题讨论】:

    标签: django bash cron tail tee


    【解决方案1】:

    使用:

    #!/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'
    

    【讨论】:

    • 嗨!感谢您的回答。它有点工作,但是当我按照你的建议使用 TMPFILE 和 grep -e 时,如果打印出我在上面的问题中编辑的内容。大多数情况下,问题出在>>> 之间,现在它可以工作了,但是DATE 的grep 不起作用,其中包含INFO 的整个文件都会被打印出来。 :(
    • 是否:grep -e INFO -e $DATE logs/product_updates.log 生成正确的输出? $TMPFILE 中的输出可能需要 grep 的 --line-buffered 选项
    • 它已通过>> 简单地修复,现在效果很好!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多