File: //bigscoots/wpo/extras/wp_cron.sh
#!/bin/bash
PATH=/usr/lib64/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
WPCLIFLAGS="--allow-root --require=/bigscoots/includes/err_report.php"
WP_PATH="$1"
DEBUG_LOG_DIR="/root/.bigscoots/wpcron"
DOMAIN=$(basename $(dirname $WP_PATH))
source /bigscoots/includes/common.sh
source /bigscoots/wpo/extras/bigscoots.menu
if [[ "$*" == *"--stagger"* ]] && [[ "$*" != *"--debug"* ]]; then
# Stagger up to 4 minutes (240s) for a 5-min cron interval
sleep $(( RANDOM % 240 ))
fi
# Check if the script was called with --debug
if [[ "$2" == "--debug" ]]
then
DEBUG=true
EPOCHTIME=$(date +%s)
DEBUG_LOG="$DEBUG_LOG_DIR/$DOMAIN/$EPOCHTIME.log"
mkdir -p "$(dirname "$DEBUG_LOG")"
exec > >(tee -a "$DEBUG_LOG")
exec 2>&1
else
DEBUG=false
# Redirect all output (stdout and stderr) to /dev/null
exec > /dev/null 2>&1
fi
# Define the PHP config file pattern
PHP_CONFIG_PATTERN="/usr/local/nginx/conf/php.*.conf"
# Define the PHP binary directory
PHP_BINARY_DIR="/opt/remi"
# Initialize WPCLI variable
WPCLI=""
# Initialize an array to store PHP versions found
PHP_VERSIONS=()
# Extract PHP versions from PHP config files
while IFS= read -r line
do
# Extract the PHP version from the config file path
version=$(echo "$line" | grep -oP 'php\d+' | cut -c 4-)
# Add the version to the array if found
if [ -n "$version" ]
then
PHP_VERSIONS+=("$version")
fi
done < <(grep "$PHP_CONFIG_PATTERN" /usr/local/nginx/conf/conf.d/"$DOMAIN".ssl.conf | awk '!/^[[:space:]]*#.*/ && !/#.*include/' | grep -o "$PHP_CONFIG_PATTERN")
# Check if any PHP version was found
if [ "${#PHP_VERSIONS[@]}" -eq 0 ]
then
# If no PHP version found, set WPCLI to default
WPCLI="/usr/bin/wp"
# Check if all PHP versions are the same
elif [ "$(printf "%s\n" "${PHP_VERSIONS[@]}" | sort -u | wc -l)" -eq 1 ]
then
# Extract the common PHP version
PHP_VERSION="${PHP_VERSIONS[0]}"
# Set the WPCLI variable
WPCLI="${PHP_BINARY_DIR}/php${PHP_VERSION}/root/usr/bin/php /usr/bin/wp"
else
OUTPUT=$(grep "$PHP_CONFIG_PATTERN" /usr/local/nginx/conf/conf.d/"$DOMAIN".ssl.conf | awk '!/^[[:space:]]*#.*/ && !/#.*include/' | grep -o "$PHP_CONFIG_PATTERN")
send_slack_alert "#wpo-alerts" ":fire:" "WP CRON: Multiple PHP Versions found." "$DOMAIN" "\`\`\`$OUTPUT\`\`\`"
exit 1
fi
[ $(stat -c "%a" /bin/wp) == "755" ] || chmod 755 /bin/wp
if wp core is-installed --network --path=${WP_PATH} ${WPCLIFLAGS} 2>/dev/null
then
WPMULTISITE=1
fi
send_slack=true
# Execute the WordPress cron job and capture output
if [[ "$WPMULTISITE" == 1 ]]
then
if ! cron_output=$(su -s /bin/bash -l nginx -c "${WPCLI} ${WPCLIFLAGS} site list --field=url --path=${WP_PATH} 2>/dev/null | xargs -i -n1 ${WPCLI} ${WPCLIFLAGS} cron event run --due-now --url={} --path=${WP_PATH} 2>/dev/null")
then
cleaned_cron_output=$(echo "$cron_output" | grep -E 'PHP (Warning|Fatal) error' | sed -e 's/<[^>]*>//g' -e 's/PHP //' -e 's/^ *//' -e 's/"/\\"/g')
if [ -z "$cleaned_cron_output" ] && [ "$send_slack" == true ] && [[ $cleaned_cron_output == *"Fatal"* || $cleaned_cron_output == *"Parse"* ]]
then
cleaned_cron_output=$cron_output
send_slack_alert "#wpo-alerts" ":warning:" "WP CRON: Failed to execute cron" "$DOMAIN" "\`\`\`$cleaned_cron_output\`\`\`"
fi
elif [[ $DEBUG == true ]]
then
echo "$cron_output" >> "$DEBUG_LOG"
fi
else
if ! cron_output=$(su -s /bin/bash -l nginx -c "${WPCLI} ${WPCLIFLAGS} cron event run --due-now --path=${WP_PATH} 2>/dev/null")
then
cleaned_cron_output=$(echo "$cron_output" | grep -E 'PHP (Fatal|Parse) error' | sed -e 's/<[^>]*>//g' -e 's/PHP //' -e 's/^ *//' -e 's/"/\\"/g')
if [ -z "$cleaned_cron_output" ] && [ "$send_slack" == true ] && [[ $cleaned_cron_output == *"Fatal"* || $cleaned_cron_output == *"Parse"* ]]
then
cleaned_cron_output=$cron_output
send_slack_alert "#wpo-alerts" ":warning:" "WP CRON: Failed to execute cron" "$DOMAIN" "\`\`\`$cleaned_cron_output\`\`\`"
fi
elif [[ $DEBUG == true ]]
then
echo "$cron_output" >> "$DEBUG_LOG"
fi
fi
rm -f "/tmp/wp-cron-${DOMAIN}.lock"