繁体   English   中英

如何在crontab中使用tee命令

[英]How to use tee command in the crontab

我在crontab中放了一个工作,每2个小时运行一次,我也想把我的bash输出的日志文件放在一个单独的文件中。

输入:

0 0-23/2 * * * /tmp/sample.sh | tee /tmp/logfile_extract_$(date '+%Y-%m-%d-%H').txt  

输出:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file

百分比(%)符号是cron中的特殊字符。 逃避%迹象。

0 0-23/2 * * * /tmp/sample.sh > /tmp/logfile_extract_$(date '+\%Y-\%m-\%d-\%H').txt 2>&1

完全有理由想要那个。 如果你愿意,你可以完全使用tee ,并且仍然能够将输出捕获到MAILTO 以此crontab为例

SHELL=/bin/bash
MAILTO=someone@example.com
0 */2 * * * php /path/script.php | tee -a /path/log.$(date +"\%Y-\%m-\%d").txt

注意Lars说的并逃脱那些百分号( %

你为什么在cron工作中使用tee 要重定向输出,您可以:

0 */2 * * * /tmp/sample.sh > /tmp/logfile_extract_$(date '+%Y-%m-%d-%H').txt 2>&1

tee需要你的tty来显示输出,而cron没有tty可用。

按照man tee

tee实用程序将标准输入复制到标准输出,从而在零个或多个文件中复制。

从您上面的评论

/tmp/logfile_extract_$(date '+%Y-%m-%d-%H').txt

是问题1)/ bin / sh实际上是bash吗? 我已经看到操作系统的“更像是一些东西”,因此bash特定的语法可能会抛出它。

0 */2 * * * /bin/bash -c '/tmp/sample.sh > /tmp/logfile_extract_$(date "+%Y-%m-%d-%H").txt 2>&1'

可能适用于这种情况。 或者你可以考虑反引号符号或者从cron调用的包装脚本

/tmp/sample.sh > /tmp/logfile_extract_$(date "+%Y-%m-%d-%H").txt 2>&1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM