From fd3585f4a3fa0a16f1917d2da2ef444d67a0c4cf Mon Sep 17 00:00:00 2001 From: James Withers Date: Fri, 13 Mar 2020 11:35:16 +0000 Subject: [PATCH] Match indicating failure returns as success The `-q` flag at https://github.com/sonassi/magestack-scripts/blob/509b84678eb7953caa7dfb60398bd1ab29852bd6/cli/magento-2/cron.sh#L49 means grep returns 0 if a match is found (http://man7.org/linux/man-pages/man1/grep.1.html). This appears to then be set as the result at https://github.com/sonassi/magestack-scripts/blob/509b84678eb7953caa7dfb60398bd1ab29852bd6/cli/magento-2/cron.sh#L50, when a result of zero is deemed a success (which it isn't in this case, as grep has found matches). This proposed change better reflects the desired logic here: even if a command (e.g. the one at https://github.com/sonassi/magestack-scripts/blob/509b84678eb7953caa7dfb60398bd1ab29852bd6/cli/magento-2/cron.sh#L44) successfully exits, it may still have logged an error, and this error (if found via a regex) needs to exit the cron wrapper itself with a value indicating failure (as well as sending an email) --- cli/magento-2/cron.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cli/magento-2/cron.sh b/cli/magento-2/cron.sh index 2be0426..056e3ba 100644 --- a/cli/magento-2/cron.sh +++ b/cli/magento-2/cron.sh @@ -2,7 +2,8 @@ EMAIL_RECIPIENT="" MAX_DURATION="43200" -ERROR_REGEX="Fatal error" +ERROR_REGEX="(PHP Fatal error|PHP Parse error|main\.ERROR)" +ERROR_REGEX_MATCH_EXIT_CODE=1 ########################################## # Do not modify anything below this line # @@ -46,8 +47,10 @@ echo "cd $REL_INSTALLDIR; $PHP_BIN update/cron.php" | timeout $MAX_DURATION fake echo "cd $REL_INSTALLDIR; $PHP_BIN bin/magento setup:cron:run" | timeout $MAX_DURATION fakechroot /usr/sbin/chroot /microcloud/domains/$DOMAIN_GROUP /bin/bash >> $LOG_FILE 2>&1; [ $? -ne 0 ] && RES=$? if [ $RES -eq 0 ] && [ -s "$LOG_FILE" ] && [[ ! -z "$ERROR_REGEX" ]]; then - grep -qE "${ERROR_REGEX}" $LOG_FILE - [ $? -ne 0 ] && RES=$? + ERROR_REGEX_MATCH_COUNT=$(grep -cE "${ERROR_REGEX}" $LOG_FILE) + if [ $ERROR_REGEX_MATCH_COUNT -ne 0 ]; then + RES=$ERROR_REGEX_MATCH_EXIT_CODE + fi fi if [ $RES -ne 0 ] && [[ ! -z "$EMAIL_RECIPIENT" ]]; then