Last active
November 5, 2025 11:26
-
-
Save hryvinskyi/4e81535c660783e4c36676a40b6ec161 to your computer and use it in GitHub Desktop.
Magento Order Monitor Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Order Monitor Script | |
| # Checks if orders have been received within the last 2 hours | |
| # Runs only between 9am and 9pm (7am + 2 hour check period) | |
| # Sends email alerts to specified addresses if no orders found | |
| # | |
| # Usage: | |
| # ./order-monitor.sh [OPTIONS] | |
| # | |
| # Options: | |
| # --test Test mode (sends test email only) | |
| # --setup-cron Setup/update cron job based on configuration | |
| # --name "Store Name" Store/project name (default: ${STORE_NAME}) | |
| # --emails "email1 email2" Alert email addresses (space-separated) | |
| # --store-ids "3" Store IDs to monitor (space-separated, default: 3) | |
| # --check-interval 2 Hours to look back for orders (default: 2) | |
| # --hours-start 7 Business hours start (default: 7) | |
| # --hours-end 21 Business hours end (default: 21) | |
| # --days "1-5" Days of week to monitor (default: 1-5 for Mon-Fri) | |
| # --period "*/30" Checking period in minutes (default: */30) | |
| # | |
| # Examples: | |
| # ./order-monitor.sh --test | |
| # ./order-monitor.sh --name "MyStore" --store-ids "3 5" --check-interval 3 | |
| # ./order-monitor.sh --setup-cron --days "1-6" --period "*/15" | |
| # Default Configuration | |
| STORE_NAME="Store Name" | |
| ALERT_EMAILS="email1@email.com email2@email.com email3@email.com" | |
| STORE_IDS="1 2 3" | |
| CHECK_INTERVAL_HOURS=2 | |
| BUSINESS_HOURS_START=7 | |
| BUSINESS_HOURS_END=21 | |
| BUSINESS_DAYS="1-5" | |
| CHECKING_PERIOD="*/30" | |
| TEST_MODE=false | |
| SETUP_CRON=false | |
| # Parse command-line arguments | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| --test|-t) | |
| TEST_MODE=true | |
| shift | |
| ;; | |
| --setup-cron|-s) | |
| SETUP_CRON=true | |
| shift | |
| ;; | |
| --name) | |
| STORE_NAME="$2" | |
| shift 2 | |
| ;; | |
| --emails) | |
| ALERT_EMAILS="$2" | |
| shift 2 | |
| ;; | |
| --store-ids) | |
| STORE_IDS="$2" | |
| shift 2 | |
| ;; | |
| --check-interval) | |
| CHECK_INTERVAL_HOURS="$2" | |
| shift 2 | |
| ;; | |
| --hours-start) | |
| BUSINESS_HOURS_START="$2" | |
| shift 2 | |
| ;; | |
| --hours-end) | |
| BUSINESS_HOURS_END="$2" | |
| shift 2 | |
| ;; | |
| --days) | |
| BUSINESS_DAYS="$2" | |
| shift 2 | |
| ;; | |
| --period) | |
| CHECKING_PERIOD="$2" | |
| shift 2 | |
| ;; | |
| --help|-h) | |
| head -n 25 "$0" | tail -n +3 | |
| exit 0 | |
| ;; | |
| *) | |
| echo "Unknown option: $1" | |
| echo "Use --help for usage information" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| # Calculate actual checking start time: business start + check interval | |
| CHECKING_START_HOUR=$((BUSINESS_HOURS_START + CHECK_INTERVAL_HOURS)) | |
| # Calculate actual checking end time: business end + check interval | |
| CHECKING_END_HOUR=$((BUSINESS_HOURS_END + CHECK_INTERVAL_HOURS)) | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| STATE_FILE="${SCRIPT_DIR}/.order-monitor-state" | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Function to log messages | |
| log() { | |
| echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | |
| } | |
| # Function to check if we're within business hours | |
| is_business_hours() { | |
| current_hour=$(date +%H) | |
| if [ "$current_hour" -ge $CHECKING_START_HOUR ] && [ "$current_hour" -lt $CHECKING_END_HOUR ]; then | |
| return 0 | |
| else | |
| return 1 | |
| fi | |
| } | |
| # Function to get order count from last N hours | |
| get_recent_order_count() { | |
| local hours=$1 | |
| local timestamp=$(date -u -v-${hours}H '+%Y-%m-%d %H:%M:%S' 2>/dev/null || date -u -d "${hours} hours ago" '+%Y-%m-%d %H:%M:%S') | |
| # Build store_id IN clause from STORE_IDS array | |
| local store_ids_clause=$(echo "$STORE_IDS" | sed 's/ /,/g') | |
| # Query Magento database for orders created in the last N hours from configured stores | |
| local count=$(cd "$SCRIPT_DIR" && php n98-magerun2.phar db:query "SELECT COUNT(*) FROM sales_order WHERE created_at >= '${timestamp}' AND store_id IN (${store_ids_clause})" --root-dir="$SCRIPT_DIR" 2>/dev/null | grep -E '^[0-9]+$' | head -1) | |
| echo "${count:-0}" | |
| } | |
| # Function to get last order details | |
| get_last_order_info() { | |
| # Build store_id IN clause from STORE_IDS array | |
| local store_ids_clause=$(echo "$STORE_IDS" | sed 's/ /,/g') | |
| local result=$(cd "$SCRIPT_DIR" && php n98-magerun2.phar db:query "SELECT CONCAT('Order #', increment_id, ' - ', customer_email, ' - ', DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s')) FROM sales_order WHERE store_id IN (${store_ids_clause}) ORDER BY created_at DESC LIMIT 1" --root-dir="$SCRIPT_DIR" 2>/dev/null | tail -1) | |
| echo "${result}" | |
| } | |
| # Function to send HTML email via PHP | |
| send_email_php() { | |
| local to_email="$1" | |
| local subject="$2" | |
| local body="$3" | |
| local is_html="${4:-false}" | |
| # Escape single quotes in body for PHP | |
| local escaped_body=$(echo "$body" | sed "s/'/\\\'/g") | |
| # Use PHP's mail function | |
| php -r " | |
| \$to = '$to_email'; | |
| \$subject = '$subject'; | |
| \$body = '$escaped_body'; | |
| if ('$is_html' === 'true') { | |
| \$headers = 'MIME-Version: 1.0' . \"\\r\\n\"; | |
| \$headers .= 'Content-type: text/html; charset=UTF-8' . \"\\r\\n\"; | |
| \$headers .= 'From: ${STORE_NAME} Order Monitor <noreply@magecloud.net>' . \"\\r\\n\"; | |
| \$headers .= 'Reply-To: noreply@magecloud.net' . \"\\r\\n\"; | |
| \$headers .= 'X-Mailer: PHP/' . phpversion(); | |
| } else { | |
| \$headers = 'From: noreply@magecloud.net' . \"\\r\\n\" . | |
| 'Reply-To: noreply@magecloud.net' . \"\\r\\n\" . | |
| 'X-Mailer: PHP/' . phpversion(); | |
| } | |
| if (mail(\$to, \$subject, \$body, \$headers)) { | |
| exit(0); | |
| } else { | |
| exit(1); | |
| } | |
| " 2>/dev/null | |
| return $? | |
| } | |
| # Function to send test email | |
| send_test_email() { | |
| local subject="✅ Test Email - ${STORE_NAME} Order Monitor" | |
| local current_time=$(date '+%Y-%m-%d %H:%M:%S') | |
| local last_order_info=$(get_last_order_info) | |
| # Create beautiful HTML email body | |
| local email_body="<!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset=\"UTF-8\"> | |
| <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> | |
| </head> | |
| <body style=\"margin: 0; padding: 0; background-color: #f4f4f4; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;\"> | |
| <table role=\"presentation\" style=\"width: 100%; border-collapse: collapse; background-color: #f4f4f4; padding: 40px 0;\"> | |
| <tr> | |
| <td align=\"center\"> | |
| <table role=\"presentation\" style=\"width: 600px; border-collapse: collapse; background-color: #ffffff; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);\"> | |
| <!-- Header --> | |
| <tr> | |
| <td style=\"background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%); padding: 40px 30px; text-align: center; border-radius: 8px 8px 0 0;\"> | |
| <div style=\"background-color: rgba(255,255,255,0.2); border-radius: 50%; width: 80px; height: 80px; margin: 0 auto 20px; display: flex; align-items: center; justify-content: center;\"> | |
| <span style=\"font-size: 48px;\">⚠️</span> | |
| </div> | |
| <h1 style=\"margin: 0; color: #ffffff; font-size: 28px; font-weight: 600;\">No Orders Alert</h1> | |
| <p style=\"margin: 10px 0 0 0; color: #ffffff; opacity: 0.9; font-size: 16px;\">Action Required</p> | |
| </td> | |
| </tr> | |
| <!-- Alert Message --> | |
| <tr> | |
| <td style=\"padding: 40px 30px;\"> | |
| <div style=\"background-color: #fff3cd; border-left: 4px solid #ff9800; padding: 16px 20px; margin-bottom: 30px; border-radius: 4px;\"> | |
| <p style=\"margin: 0; color: #856404; font-size: 16px; font-weight: 500;\">⚠ No orders have been received in the last ${CHECK_INTERVAL_HOURS} hours!</p> | |
| </div> | |
| <p style=\"color: #333; font-size: 16px; line-height: 1.6; margin: 0 0 30px 0;\"> | |
| This is an automated alert from the ${STORE_NAME} order monitoring system. Please investigate the issue immediately. | |
| </p> | |
| <!-- Alert Details --> | |
| <table style=\"width: 100%; border-collapse: collapse; margin-bottom: 30px;\"> | |
| <tr> | |
| <td style=\"padding: 12px 16px; background-color: #f8f9fa; border-bottom: 1px solid #e9ecef; color: #666; font-size: 14px; width: 35%;\"> | |
| <strong>Alert Time:</strong> | |
| </td> | |
| <td style=\"padding: 12px 16px; background-color: #ffffff; border-bottom: 1px solid #e9ecef; color: #333; font-size: 14px;\"> | |
| ${current_time} | |
| </td> | |
| </tr> | |
| <tr> | |
| <td style=\"padding: 12px 16px; background-color: #f8f9fa; border-bottom: 1px solid #e9ecef; color: #666; font-size: 14px;\"> | |
| <strong>Time Window:</strong> | |
| </td> | |
| <td style=\"padding: 12px 16px; background-color: #ffffff; border-bottom: 1px solid #e9ecef; color: #333; font-size: 14px;\"> | |
| Last ${CHECK_INTERVAL_HOURS} hours | |
| </td> | |
| </tr> | |
| <tr> | |
| <td style=\"padding: 12px 16px; background-color: #f8f9fa; color: #666; font-size: 14px;\"> | |
| <strong>Last Order:</strong> | |
| </td> | |
| <td style=\"padding: 12px 16px; background-color: #ffffff; color: #333; font-size: 14px;\"> | |
| ${last_order_info:-No orders found in database} | |
| </td> | |
| </tr> | |
| </table> | |
| <!-- Action Items --> | |
| <div style=\"background-color: #f8f9fa; padding: 24px; border-radius: 6px; margin-bottom: 20px;\"> | |
| <h2 style=\"margin: 0 0 16px 0; color: #333; font-size: 18px; font-weight: 600;\">Recommended Actions:</h2> | |
| <ul style=\"margin: 0; padding-left: 20px; color: #555; font-size: 14px; line-height: 1.8;\"> | |
| <li style=\"margin-bottom: 8px;\"><strong>Check website availability:</strong> Ensure the site is accessible and loading properly</li> | |
| <li style=\"margin-bottom: 8px;\"><strong>Verify payment gateway:</strong> Confirm payment processing is working correctly</li> | |
| <li style=\"margin-bottom: 8px;\"><strong>Review system logs:</strong> Check for any errors or warnings in application logs</li> | |
| <li style=\"margin-bottom: 8px;\"><strong>Monitor traffic:</strong> Verify that the site is receiving normal visitor traffic</li> | |
| <li><strong>Test checkout process:</strong> Attempt a test purchase to identify any issues</li> | |
| </ul> | |
| </div> | |
| <div style=\"background-color: #e3f2fd; padding: 16px 20px; border-radius: 4px; border-left: 4px solid #2196f3;\"> | |
| <p style=\"margin: 0; color: #1565c0; font-size: 13px; line-height: 1.6;\"> | |
| <strong>Note:</strong> This alert will not be sent again for ${CHECK_INTERVAL_HOURS} hours to prevent spam. The monitoring system will continue to check for new orders every 30 minutes during business hours (7am-9pm). | |
| </p> | |
| </div> | |
| </td> | |
| </tr> | |
| <!-- Footer --> | |
| <tr> | |
| <td style=\"background-color: #f8f9fa; padding: 20px 30px; text-align: center; border-radius: 0 0 8px 8px; border-top: 1px solid #e9ecef;\"> | |
| <p style=\"margin: 0 0 8px 0; color: #666; font-size: 12px;\"> | |
| Automated alert from <strong>${STORE_NAME} Order Monitor</strong> | |
| </p> | |
| <p style=\"margin: 0; color: #999; font-size: 11px;\"> | |
| This is an automated message. Please do not reply to this email. | |
| </p> | |
| </td> | |
| </tr> | |
| </table> | |
| </td> | |
| </tr> | |
| </table> | |
| </body> | |
| </html>" | |
| log "${YELLOW}TEST MODE: Sending test email...${NC}" | |
| # Send test email to each recipient | |
| for email in $ALERT_EMAILS; do | |
| # Try PHP mail first with HTML | |
| if send_email_php "$email" "$subject" "$email_body" "true"; then | |
| log "${GREEN}Test email sent successfully to: $email${NC}" | |
| # Fallback to system mail command | |
| elif echo "$email_body" | mail -s "$subject" "$email" 2>/dev/null; then | |
| log "${GREEN}Test email sent successfully to: $email (via mail command)${NC}" | |
| else | |
| log "${RED}Failed to send test email to: $email${NC}" | |
| log "${YELLOW}Trying alternative: Use Magento's email system...${NC}" | |
| # Last resort: Try using Magento CLI to send email | |
| if [ -f "${SCRIPT_DIR}/bin/magento" ]; then | |
| log "${YELLOW}Note: You may need to configure Magento email settings${NC}" | |
| else | |
| log "${RED}Make sure PHP mail() function or 'mail' command is configured${NC}" | |
| fi | |
| fi | |
| done | |
| } | |
| # Function to send email alert | |
| send_alert() { | |
| local subject="⚠️ No Orders Received - ${STORE_NAME} Alert" | |
| local last_order_info=$(get_last_order_info) | |
| local current_time=$(date '+%Y-%m-%d %H:%M:%S') | |
| # Create beautiful HTML alert email | |
| local email_body="<!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset=\"UTF-8\"> | |
| <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> | |
| </head> | |
| <body style=\"margin: 0; padding: 0; background-color: #f4f4f4; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;\"> | |
| <table role=\"presentation\" style=\"width: 100%; border-collapse: collapse; background-color: #f4f4f4; padding: 40px 0;\"> | |
| <tr> | |
| <td align=\"center\"> | |
| <table role=\"presentation\" style=\"width: 600px; border-collapse: collapse; background-color: #ffffff; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);\"> | |
| <!-- Header --> | |
| <tr> | |
| <td style=\"background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%); padding: 40px 30px; text-align: center; border-radius: 8px 8px 0 0;\"> | |
| <div style=\"background-color: rgba(255,255,255,0.2); border-radius: 50%; width: 80px; height: 80px; margin: 0 auto 20px; display: flex; align-items: center; justify-content: center;\"> | |
| <span style=\"font-size: 48px;\">⚠️</span> | |
| </div> | |
| <h1 style=\"margin: 0; color: #ffffff; font-size: 28px; font-weight: 600;\">No Orders Alert</h1> | |
| <p style=\"margin: 10px 0 0 0; color: #ffffff; opacity: 0.9; font-size: 16px;\">Action Required</p> | |
| </td> | |
| </tr> | |
| <!-- Alert Message --> | |
| <tr> | |
| <td style=\"padding: 40px 30px;\"> | |
| <div style=\"background-color: #fff3cd; border-left: 4px solid #ff9800; padding: 16px 20px; margin-bottom: 30px; border-radius: 4px;\"> | |
| <p style=\"margin: 0; color: #856404; font-size: 16px; font-weight: 500;\">⚠ No orders have been received in the last ${CHECK_INTERVAL_HOURS} hours!</p> | |
| </div> | |
| <p style=\"color: #333; font-size: 16px; line-height: 1.6; margin: 0 0 30px 0;\"> | |
| This is an automated alert from the ${STORE_NAME} order monitoring system. Please investigate the issue immediately. | |
| </p> | |
| <!-- Alert Details --> | |
| <table style=\"width: 100%; border-collapse: collapse; margin-bottom: 30px;\"> | |
| <tr> | |
| <td style=\"padding: 12px 16px; background-color: #f8f9fa; border-bottom: 1px solid #e9ecef; color: #666; font-size: 14px; width: 35%;\"> | |
| <strong>Alert Time:</strong> | |
| </td> | |
| <td style=\"padding: 12px 16px; background-color: #ffffff; border-bottom: 1px solid #e9ecef; color: #333; font-size: 14px;\"> | |
| ${current_time} | |
| </td> | |
| </tr> | |
| <tr> | |
| <td style=\"padding: 12px 16px; background-color: #f8f9fa; border-bottom: 1px solid #e9ecef; color: #666; font-size: 14px;\"> | |
| <strong>Time Window:</strong> | |
| </td> | |
| <td style=\"padding: 12px 16px; background-color: #ffffff; border-bottom: 1px solid #e9ecef; color: #333; font-size: 14px;\"> | |
| Last ${CHECK_INTERVAL_HOURS} hours | |
| </td> | |
| </tr> | |
| <tr> | |
| <td style=\"padding: 12px 16px; background-color: #f8f9fa; color: #666; font-size: 14px;\"> | |
| <strong>Last Order:</strong> | |
| </td> | |
| <td style=\"padding: 12px 16px; background-color: #ffffff; color: #333; font-size: 14px;\"> | |
| ${last_order_info:-No orders found in database} | |
| </td> | |
| </tr> | |
| </table> | |
| <!-- Action Items --> | |
| <div style=\"background-color: #f8f9fa; padding: 24px; border-radius: 6px; margin-bottom: 20px;\"> | |
| <h2 style=\"margin: 0 0 16px 0; color: #333; font-size: 18px; font-weight: 600;\">Recommended Actions:</h2> | |
| <ul style=\"margin: 0; padding-left: 20px; color: #555; font-size: 14px; line-height: 1.8;\"> | |
| <li style=\"margin-bottom: 8px;\"><strong>Check website availability:</strong> Ensure the site is accessible and loading properly</li> | |
| <li style=\"margin-bottom: 8px;\"><strong>Verify payment gateway:</strong> Confirm payment processing is working correctly</li> | |
| <li style=\"margin-bottom: 8px;\"><strong>Review system logs:</strong> Check for any errors or warnings in application logs</li> | |
| <li style=\"margin-bottom: 8px;\"><strong>Monitor traffic:</strong> Verify that the site is receiving normal visitor traffic</li> | |
| <li><strong>Test checkout process:</strong> Attempt a test purchase to identify any issues</li> | |
| </ul> | |
| </div> | |
| <div style=\"background-color: #e3f2fd; padding: 16px 20px; border-radius: 4px; border-left: 4px solid #2196f3;\"> | |
| <p style=\"margin: 0; color: #1565c0; font-size: 13px; line-height: 1.6;\"> | |
| <strong>Note:</strong> This alert will not be sent again for ${CHECK_INTERVAL_HOURS} hours to prevent spam. The monitoring system will continue to check for new orders every 30 minutes during business hours (7am-9pm). | |
| </p> | |
| </div> | |
| </td> | |
| </tr> | |
| <!-- Footer --> | |
| <tr> | |
| <td style=\"background-color: #f8f9fa; padding: 20px 30px; text-align: center; border-radius: 0 0 8px 8px; border-top: 1px solid #e9ecef;\"> | |
| <p style=\"margin: 0 0 8px 0; color: #666; font-size: 12px;\"> | |
| Automated alert from <strong>${STORE_NAME} Order Monitor</strong> | |
| </p> | |
| <p style=\"margin: 0; color: #999; font-size: 11px;\"> | |
| This is an automated message. Please do not reply to this email. | |
| </p> | |
| </td> | |
| </tr> | |
| </table> | |
| </td> | |
| </tr> | |
| </table> | |
| </body> | |
| </html>" | |
| # Send email to each recipient | |
| for email in $ALERT_EMAILS; do | |
| # Try PHP mail first with HTML | |
| if send_email_php "$email" "$subject" "$email_body" "true"; then | |
| log "Alert email sent to: $email" | |
| # Fallback to system mail command | |
| elif echo "$email_body" | mail -s "$subject" "$email" 2>/dev/null; then | |
| log "Alert email sent to: $email (via mail command)" | |
| else | |
| log "${RED}Failed to send alert email to: $email${NC}" | |
| fi | |
| done | |
| # Update state file to prevent duplicate alerts | |
| echo "$(date +%s)" > "$STATE_FILE" | |
| } | |
| # Function to check if alert was already sent recently | |
| should_send_alert() { | |
| if [ ! -f "$STATE_FILE" ]; then | |
| return 0 | |
| fi | |
| local last_alert_time=$(cat "$STATE_FILE") | |
| local current_time=$(date +%s) | |
| local time_diff=$((current_time - last_alert_time)) | |
| local alert_cooldown=$((CHECK_INTERVAL_HOURS * 3600)) | |
| # Only send alert if cooldown period has passed | |
| if [ $time_diff -ge $alert_cooldown ]; then | |
| return 0 | |
| else | |
| return 1 | |
| fi | |
| } | |
| # Function to setup or update cron job | |
| setup_cron() { | |
| log "${YELLOW}=== CRON SETUP MODE ===${NC}" | |
| # Build command with only necessary runtime arguments | |
| # Note: --period and --days are not needed since they're in the cron schedule | |
| local cmd="${SCRIPT_DIR}/order-monitor.sh" | |
| cmd="${cmd} --name \"${STORE_NAME}\"" | |
| cmd="${cmd} --emails \"${ALERT_EMAILS}\"" | |
| cmd="${cmd} --store-ids \"${STORE_IDS}\"" | |
| cmd="${cmd} --check-interval ${CHECK_INTERVAL_HOURS}" | |
| cmd="${cmd} --hours-start ${BUSINESS_HOURS_START}" | |
| cmd="${cmd} --hours-end ${BUSINESS_HOURS_END}" | |
| # Add log file redirection | |
| local log_file="${SCRIPT_DIR}/var/log/order-monitor.log" | |
| cmd="${cmd} >> ${log_file} 2>&1" | |
| # Build cron expression with description comment | |
| # Cron hours: CHECKING_START_HOUR to CHECKING_END_HOUR-1 (since cron is inclusive and we want to stop before CHECKING_END_HOUR) | |
| local cron_comment="# ${STORE_NAME} Order Monitor: Check stores ${STORE_IDS} every ${CHECKING_PERIOD} min, ${CHECKING_START_HOUR}:00-${CHECKING_END_HOUR}:00, days ${BUSINESS_DAYS}" | |
| local cron_expression="${CHECKING_PERIOD} ${CHECKING_START_HOUR}-$((CHECKING_END_HOUR-1)) * * ${BUSINESS_DAYS} ${cmd}" | |
| log "Cron expression:" | |
| log "${cron_comment}" | |
| log "${cron_expression}" | |
| log "" | |
| log "This will run the order monitor:" | |
| log " - Store name: ${STORE_NAME}" | |
| log " - Every: ${CHECKING_PERIOD} minutes" | |
| log " - Hours: ${CHECKING_START_HOUR}:00 - ${CHECKING_END_HOUR}:00" | |
| log " - Days: ${BUSINESS_DAYS} (0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat)" | |
| log " - Store IDs: ${STORE_IDS}" | |
| log " - Check interval: ${CHECK_INTERVAL_HOURS} hours" | |
| log " - Alert emails: ${ALERT_EMAILS}" | |
| log "" | |
| # Check if cron entry already exists | |
| local existing_cron=$(crontab -l 2>/dev/null | grep -F "${SCRIPT_DIR}/order-monitor.sh") | |
| if [ -n "$existing_cron" ]; then | |
| log "${YELLOW}Found existing cron entry:${NC}" | |
| log "$existing_cron" | |
| log "" | |
| read -p "Do you want to update it? (y/n): " -n 1 -r | |
| echo | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| log "${YELLOW}Cron setup cancelled.${NC}" | |
| exit 0 | |
| fi | |
| # Remove old entries (both comment and command) | |
| crontab -l 2>/dev/null | grep -v " Order Monitor" | grep -v -F "${SCRIPT_DIR}/order-monitor.sh" | crontab - | |
| log "${GREEN}Removed old cron entries${NC}" | |
| fi | |
| # Add new cron entry with comment | |
| (crontab -l 2>/dev/null; echo "$cron_comment"; echo "$cron_expression") | crontab - | |
| if [ $? -eq 0 ]; then | |
| log "${GREEN}Cron job successfully added!${NC}" | |
| log "" | |
| log "Current crontab entries:" | |
| crontab -l | grep -A1 " Order Monitor" | |
| else | |
| log "${RED}Failed to add cron job${NC}" | |
| exit 1 | |
| fi | |
| } | |
| # Main execution | |
| main() { | |
| # Check if setup cron mode is enabled | |
| if [ "$SETUP_CRON" = true ]; then | |
| setup_cron | |
| exit 0 | |
| fi | |
| # Check if test mode is enabled | |
| if [ "$TEST_MODE" = true ]; then | |
| log "${YELLOW}=== TEST MODE ===${NC}" | |
| send_test_email | |
| log "${GREEN}Test complete. Check your email at: ${ALERT_EMAILS}${NC}" | |
| exit 0 | |
| fi | |
| # Normal mode operation | |
| log "Starting order monitor check..." | |
| # Check if we're in business hours | |
| if ! is_business_hours; then | |
| log "${YELLOW}Outside business hours (7am-9pm). Skipping check.${NC}" | |
| exit 0 | |
| fi | |
| log "Within business hours. Checking for recent orders..." | |
| # Get order count from last N hours | |
| order_count=$(get_recent_order_count "$CHECK_INTERVAL_HOURS") | |
| log "Orders in last ${CHECK_INTERVAL_HOURS} hours: ${order_count}" | |
| if [ "$order_count" -eq 0 ]; then | |
| log "${RED}No orders found in the last ${CHECK_INTERVAL_HOURS} hours!${NC}" | |
| if should_send_alert; then | |
| log "Sending alert emails..." | |
| send_alert | |
| log "${RED}Alert sent to: ${ALERT_EMAILS}${NC}" | |
| else | |
| log "${YELLOW}Alert already sent recently. Skipping to avoid spam.${NC}" | |
| fi | |
| else | |
| log "${GREEN}Orders detected. System operating normally.${NC}" | |
| # Clear state file if orders are present | |
| rm -f "$STATE_FILE" | |
| fi | |
| log "Check complete." | |
| } | |
| # Run main function | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment