将 cron 输出发送到从程序名称派生的文件名

将 cron 输出发送到从程序名称派生的文件名

我测试了以下内容,但没有运气。还有其他想法吗?

#cron test, I would like to generate a stderr logfile name based on $0, but $0 value is "/bin/bash"
#why do I want this? Because some program names are quite long, and for standardization
04 10 * * * ~/bin/cron-test >> /tmp/blah 2>>~/.cronlogs/$(basename $0)  # $0 is "/bin/bash"
05 10 * * * ~/bin/cron-test >> /tmp/blah 2>>~/.cronlogs/$(basename $1)  # $1 is empty?
06 10 * * * ~/bin/cron-test >> /tmp/blah 2>>~/.cronlogs/$(basename $2)  # $2 is empty?

答案1

您可以编写一个包装器脚本将输出发送到某个特定位置。

$ cat logwrap.sh
#!/bin/bash
logname=$(basename -- "$1").log
exec "$@" 2>> "~/.cronlogs/$logname"

现在,logwrap.sh id some command将运行some command并将其输出发送到/work/logs/some.log,因此这会将其输出发送到/work/logs/cron-test.log

04 10 * * * /path/to/logwrap.sh ~/bin/cron-test 

相关内容