我有一些 PHP 脚本为我做一些工作并打印一些日志信息。这是调用的结构:
crontab
*/3 * * * * sleep 180 && cd /var/www/tasks && ./wrapper.sh start "/usr/bin/php stat-import.php" stat-import >> stat-import.log
包装器.sh
#!/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 而是进入日志文件?嗯?