I’ve some PHP scripts doing some work for me and printing some logging informations. This is the structure of calling:
Crontab
*/3 * * * * sleep 180 && cd /var/www/tasks && ./wrapper.sh start "/usr/bin/php stat-import.php" stat-import >> stat-import.log
wrapper.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);
By default only syntax errors in wrapper.sh or in my PHP script result in a mail by CRON. My user defined errors in stat-import.php aren’t passed to CRON but go into log file? Huh?
By default, PHP errors are printed to
stdout, which your cron is redirecting to the log file. You need to print your errors tostderrinstead, so they will be mailed by the cron daemon: display_errors setting in PHP docs