我有一些PHP脚本为我做一些工作,并打印一些日志记录信息。这是调用的结构:
Crontab
*/3 * * * * sleep 180 && cd /var/www/tasks && ./wrapper.sh start "/usr/bin/php stat-import.php" stat-import >> stat-import.log
包装器
#!/bin/bash
function start
{
WRAP_CMD="$1"
WRAP_DESC="$2"
ARGS=($WRAP_CMD)
if [[ ( $WRAP_DESC ) && ( -n $WRAP_DESC ) ]]
then
OUT_DESC="$WRAP_DESC"
else
OUT_DESC="$WRAP_CMD"
fi
PID=`ps axw -o pid,command | grep "$WRAP_CMD" | grep -v grep | grep -v "$0" | awk '{print $1}' | awk '{print $1}'`
if [[ ( $PID ) && ( -n $PID ) ]]
then
echo `date +'%Y-%m-%d %H:%M:%S'`" INFO - $OUT_DESC already running"
else
echo `date +'%Y-%m-%d %H:%M:%S'`" INFO - $OUT_DESC started"
$WRAP_CMD
ECODE=$?
echo `date +'%Y-%m-%d %H:%M:%S'`" INFO - $OUT_DESC finished"
exit $ECODE
fi
}
function stop
{
[...]
}
function main
{
if [[ ( $# < 2 ) || ( $# > 3 ) ]]
then
echo "Usage: $0 [start|stop] COMMAND [DESCRIPTION]"
exit 0
fi
if [ $1 == "start" ]
then
start "$2" "$3"
elif [ $1 == "stop" ]
then
stop "$2" "$3"
else
echo "Usage: $0 [start|stop] COMMAND [DESCRIPTION]"
fi
exit 0
}
# Script execution:
main "$@"
stat-import.php
<?php
die("error message");
// OR
exit(127);
// OR
trigger_error("error_message", E_USER_ERROR);
默认情况下,只有
wrapper.sh
或我的PHP脚本中的语法错误会导致CRON发送邮件。我的用户定义的stat-import.php
错误没有传递给CRON,但进入了日志文件? ??
最佳答案
默认情况下,PHP错误会打印到stdout
,您的cron会将其重定向到日志文件。您需要将错误打印到stderr
,以便将这些错误由cron守护程序邮寄:display_errors setting in PHP docs
关于php - PHP脚本中的错误不会传递回CRON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10783873/