Skip to content

Instantly share code, notes, and snippets.

@dsp76
Last active September 20, 2024 16:35
Show Gist options
  • Save dsp76/b0d91f12f2f0fe533fc7ca4c32384d8c to your computer and use it in GitHub Desktop.
Save dsp76/b0d91f12f2f0fe533fc7ca4c32384d8c to your computer and use it in GitHub Desktop.
Mautic cron script with mail rate limit - Mautic v4
#!/bin/bash
# Set the working directory to the script's directory
cd "$(dirname "$0")"
# Variables for the script
phpinterpreter="php -d memory_limit=1G"
pathtoconsole="../code/bin/console"
lockfile="/tmp/cronjob.lock"
# Directory for log files
log_dir="./logs" # Directory for logs, adjust as needed
mkdir -p "$log_dir" # Create the directory if it doesn't exist
# Number of log files to keep
max_logs=40 # Number of log files to keep
# Email sending settings
emails_per_batch=14
segments_update_limit=900
campaigns_rebuild_limit=300
broadcasts_send_limit=800
import_limit=500
# Loop limit
max_loops=60
current_loop=0
milliseconds_per_second=1000
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Locking mechanism
if [ -f "$lockfile" ]; then
# Colored output in the console
echo "${RED}Script is already running.${NC}"
# Output without colors to the log file
echo "Script is already running." >> "$log_file"
exit 1
fi
touch "$lockfile"
trap 'rm -f "$lockfile"; exit' INT TERM EXIT
# Log file for the current run
log_file="$log_dir/$(date +'%Y%m%d_%H%M%S').log"
# Function to execute Mautic commands and log the results
execute_command() {
local command="$1"
local full_command="$phpinterpreter $pathtoconsole $command"
# Colored output in the terminal (stdout)
echo "${BLUE}Executing: $command${NC}"
echo "${YELLOW}Raw Command: $full_command${NC}"
# Output without colors to the log file
echo "Executing: $command" >> "$log_file"
echo "Raw Command: $full_command" >> "$log_file"
# Execute the command and capture both stdout and stderr
output=$($full_command 2>&1)
# Colored output in the terminal (stdout and stderr)
if [ $? -eq 0 ]; then
echo "${GREEN}$output${NC}"
else
echo "${RED}$output${NC}" >&2
fi
# Output without colors to the log file
echo "$output" >> "$log_file"
return $? # Return the exit status of the command
}
# Function to clean up old log files
cleanup_logs() {
while [ $(ls -1 "$log_dir"/*.log 2>/dev/null | wc -l) -gt "$max_logs" ]; do
oldest_log=$(ls -t "$log_dir"/*.log | tail -1)
echo "Deleting old log file: $oldest_log"
rm -f "$oldest_log"
done
}
# Calculate sleep time in microseconds
calculate_sleep_time() {
local duration=$1
local sleep_time=$(( (emails_per_batch * milliseconds_per_second) - duration ))
echo "$sleep_time"
}
# Execute other commands before the email loop
execute_command "mautic:segments:update --batch-limit=$segments_update_limit"
execute_command "mautic:campaigns:rebuild --batch-limit=$campaigns_rebuild_limit"
execute_command "mautic:campaigns:trigger"
execute_command "mautic:broadcasts:send --batch=$broadcasts_send_limit"
# Loop for sending emails
while [ $current_loop -lt $max_loops ]; do
current_loop=$((current_loop + 1))
start_time=$(date +%s%3N)
output=$(execute_command "mautic:emails:send --message-limit=$emails_per_batch")
end_time=$(date +%s%3N)
duration=$((end_time - start_time))
sent_emails=$(echo "$output" | grep -oP '\b\d+(?= emails sent)' || echo 0)
if [ "$sent_emails" -lt "$emails_per_batch" ]; then
echo "${RED}Less than $emails_per_batch emails sent. Exiting loop.${NC}"
echo "Less than $emails_per_batch emails sent. Exiting loop." >> "$log_file"
break
fi
sleep_time=$(calculate_sleep_time "$duration")
[[ "$sleep_time" -gt 0 ]] && usleep "$sleep_time"
done
execute_command "mautic:unusedip:delete"
execute_command "mautic:import --limit=$import_limit"
execute_command "mautic:webhooks:process"
execute_command "mautic:reports:scheduler"
# Cleanup old log files
cleanup_logs
# Unlock the script at the end
rm -f "$lockfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment