Edit File: scripts.sh
#!/bin/bash # --- Execution Time Logging Section --- EXECUTION_LOG="/usr/local/cpanel/whostmgr/docroot/cgi/cptools/execution.log" start_time=$(date +%s) log_execution_time() { end_time=$(date +%s) elapsed=$(( end_time - start_time )) minutes=$(( elapsed / 60 )) seconds=$(( elapsed % 60 )) # EXACTLY ONE date call formatted_date=$(date "+%d-%B-%Y %I:%M %p %Z") echo "$formatted_date : ${minutes} mins ${seconds} sec" >> "$EXECUTION_LOG" # Remove the start file if it exists if [ -f "/usr/local/cptools/scripts.start" ]; then rm -f /usr/local/cptools/scripts.start fi } # Create execution log only if it doesn't already exist. if [ ! -f "$EXECUTION_LOG" ]; then touch "$EXECUTION_LOG" chmod 666 "$EXECUTION_LOG" fi trap log_execution_time EXIT # --- TEST DELAY (for debugging, remove when not needed) --- #sleep 300 # --- Existing Script Tasks --- TOP_FILES_LAST_RUN="/usr/local/cpanel/whostmgr/docroot/cgi/cptools/tmp/cptools_get_top_files_last_run" RESELLER_TOP_FILES_LAST_RUN="/usr/local/cpanel/whostmgr/docroot/cgi/cptools/tmp/cptools_get_reseller_top_files_last_run" LOG_FILE="/usr/local/cpanel/whostmgr/docroot/cgi/cptools/update.log" SETTINGS_FILE="/usr/local/cptools/settings.conf" mkdir -p "/usr/local/cptools/tmp/" log_update() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } REMOTE_VERSION=$(curl -s "https://cpanelplugin.com/cptools/version") LOCAL_VERSION=$(cat "/usr/local/cptools/version") touch "$TOP_FILES_LAST_RUN" "$RESELLER_TOP_FILES_LAST_RUN" chmod 666 "$TOP_FILES_LAST_RUN" "$RESELLER_TOP_FILES_LAST_RUN" # -------------------------- # Threshold Handling Section # -------------------------- # Default threshold percentage is 20 if not defined. DEFAULT_THRESHOLD=20 THRESHOLD_PERCENT="$DEFAULT_THRESHOLD" if [[ -f "$SETTINGS_FILE" ]]; then # Use jq to extract the threshold key from the JSON settings file. # Make sure jq is installed on your system. THRESHOLD_PERCENT=$(jq -r '.threshold' "$SETTINGS_FILE") # Fallback to default if not numeric if ! [[ "$THRESHOLD_PERCENT" =~ ^[0-9]+$ ]]; then THRESHOLD_PERCENT=$DEFAULT_THRESHOLD fi fi # Convert percentage to decimal multiplier (e.g., 20 becomes 0.20) THRESHOLD_MULTIPLIER=$(echo "scale=2; $THRESHOLD_PERCENT / 100" | bc) CPU_LOAD=$(uptime | awk -F'load average: ' '{print $2}' | awk -F', ' '{print $1}') CPU_CORES=$(nproc) MAX_LOAD=$(echo "$CPU_CORES * $THRESHOLD_MULTIPLIER" | bc) if (( $(echo "$CPU_LOAD > $MAX_LOAD" | bc -l) )); then echo "High CPU load ($CPU_LOAD). Skipping execution." | systemd-cat -t cptools_scheduler -p warning log_update "Execution skipped due to high CPU load ($CPU_LOAD)." exit 1 fi CURRENT_TIME=$(date +%s) if [[ ! -s "$TOP_FILES_LAST_RUN" ]] || (( CURRENT_TIME - $(cat "$TOP_FILES_LAST_RUN") >= 28800 )); then echo "Executing get_top_files.sh" log_update "Running get_top_files.sh" /usr/local/cptools/get_top_files.sh echo "$CURRENT_TIME" > "$TOP_FILES_LAST_RUN" else LAST_RUN_TIME=$(date -d "@$(cat $TOP_FILES_LAST_RUN)" "+%Y-%m-%d %H:%M:%S") log_update "Skipping get_top_files.sh (Last run: $LAST_RUN_TIME)" fi if [[ ! -s "$RESELLER_TOP_FILES_LAST_RUN" ]] || (( CURRENT_TIME - $(cat "$RESELLER_TOP_FILES_LAST_RUN") >= 43200 )); then echo "Executing get_reseller_top_files.sh" log_update "Running get_reseller_top_files.sh" /usr/local/cptools/get_reseller_top_files.sh echo "$CURRENT_TIME" > "$RESELLER_TOP_FILES_LAST_RUN" else LAST_RUN_TIME=$(date -d "@$(cat $RESELLER_TOP_FILES_LAST_RUN)" "+%Y-%m-%d %H:%M:%S") log_update "Skipping get_reseller_top_files.sh (Last run: $LAST_RUN_TIME)" fi log_update "Scripts executed successfully." echo "Scripts executed successfully." | systemd-cat -t cptools_scheduler -p info