审查一个crontab脚本来添加时间戳到每分钟运行; 有什么build议么?

我已经有一个crontab作业设置好了,

* * * * * /home/username/public_html/domain.tld/production/scripts/cron_runner.sh 

我的目标是添加一些时间戳从cron输出,所以我可以更轻松地findwebapp内部cron系统中的某些故障点。 有时我会收到消息,有时候我不会看到,我只想执行一个完整的检查,我已经在这里正确地创build了这个脚本:

 #!/usr/bin/env bash # # Script Name: cron_runner.sh # echo "Cron Execution @ `date`: " >>/home/username/public_html/domain.tld/production/var/log/crontab_output.log 2>&1 cd /home/username/public_html/domain.tld/production && /usr/local/bin/php /home/username/public_html/domain.tld/production/cron.php >>/home/username/public_html/domain.tld/production/var/log/crontab_output.log 2>&1 echo "==== END CRON EXEC ====" >>/home/username/public_html/domain.tld/production/var/log/crontab_output.log 2>&1 

有什么build议么?

您可以将脚本通过pipe道传递给moreutils ,这会将时间戳添加到每行中,例如:

  (cd /home/username/public_html/domain.tld/production && /usr/local/bin/php /home/username/public_html/domain.tld/production/cron.php) | ts >>/home/username/public_html/domain.tld/production/var/log/crontab_output.log 2>&1 

另外,每次crond执行一个作业时,通常都将它logging在/var/log/cron (取决于* nix的味道),所以如果适用于你,我应该先检查一下。