我有一个crontab条目:
00 10 * * * test.sh>>output_log 2>>error_log
但我想也包括日期为output_log和error_log在每一天的开始行。
我怎样才能做到这一点?
我试过:
{ echo `date` && . test.sh; }>>output_log 2>>error_log
Run Code Online (Sandbox Code Playgroud)
但它仅在 output_log 中包含日期,因为echo date不被视为stderr因此不包含在2.
我想要这样的伪代码:
{ echo `date` && . test.sh }>>output_log { echo `date` plus 2's content } >>error_log
Run Code Online (Sandbox Code Playgroud)
最简单的:
0 10 * * * { date; date >&2; test.sh; } >> output_log 2>> error_log
Run Code Online (Sandbox Code Playgroud)
使用ts来自 Moreutils的实用程序可能也很有趣,它会在每一行前面加上时间戳:
SHELL=/bin/bash
0 10 * * * test.sh > >(ts >> output_log) 2> >(ts >> error_log)
Run Code Online (Sandbox Code Playgroud)
(注意使用/bin/bash,因为最基本的 POSIX/bin/sh不支持>()重定向构造。)