如何在 crontab 中使用 tee 命令

Shi*_*wal 5 linux bash shell cron tee

我在 crontab 中放置了一个作业,每 2 小时运行一次,我还希望将 bash 输出的日志文件放在一个单独的文件中。

输入:

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

输出:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 1

你为什么tee在 cron 作业中使用。要重定向输出,您可以执行以下操作:

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

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

按照man tee

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