Hei*_*erg 2 cron zsh amazon-linux
在我的 crontab 中,我设置了以下 bash 函数并将其应用于我的工作。表示要在日志中添加时间戳。
adddate() {
while IFS= read -r line; do
printf '%s %s\n' "$(date)" "$line";
done
}
30 06 * * * root $binPath/zsh/test.zsh | adddate 1>>$logPath/log.csv 2>>$errorLogPath/error.txt
Run Code Online (Sandbox Code Playgroud)
但是当我看到error.txt
bash 功能不能很好地工作时。
/bin/bash: adddate: command not found
这一切的根本原因在哪里呢?
如果有人有意见,请告诉我。
谢谢
Cron 不接受 shell 函数,创建一个类似的脚本
#!/bin/bash
adddate() {
while IFS= read -r line; do
printf '%s %s\n' "$(date)" "$line";
done
}
$binPath/zsh/test.zsh | adddate 1>>$logPath/log.csv 2>>$errorLogPath/error.txt
Run Code Online (Sandbox Code Playgroud)
并将其放入 cron 中。
(我假设您在这里使用了$binPath
and$logPath
来解决这个问题。如果不是这种情况,您必须在脚本中设置它们)
SHELL=/bin/bash
在你的设置中crontab
可能是使用 shell 函数的一种方法。
(我没有尝试过,如果它有效的话我会感到惊讶)。但即使它有效,我也肯定不会建议它。