1. Update immediately if your public IP address changes.
  2. Force an update if a certain amount of time has passed, even if the IP hasn’t changed, to satisfy the 90-day activity requirement.
#!/bin/bash
 
# --- Configuration ---
API_KEY="YOUR_KEY_HERE" # Replace with your actual key from myaddr.tools
LOG_FILE="/var/log/myaddr_tools_update.log"
 
# We'll store our status info in a temporary directory
STATE_DIR="/tmp/myaddr_tools"
LAST_IP_FILE="${STATE_DIR}/last_public_ip.txt"
LAST_UPDATE_TIMESTAMP_FILE="${STATE_DIR}/last_update_timestamp.txt"
 
# Force an update if the last one was more than this many days ago.
# Set to 80 as a safe buffer before the 90-day limit.
FORCE_UPDATE_DAYS=80
 
# --- Initialization ---
# Ensure the state directory exists
mkdir -p "$STATE_DIR"
 
# --- Main Logic ---
 
# Get current public IP and current time as a Unix timestamp
CURRENT_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
CURRENT_TIMESTAMP=$(date +%s)
 
# Check if we got a valid IP
if [ -z "$CURRENT_IP" ]; then
    echo "$(date): Could not determine current public IP. Exiting." >> "$LOG_FILE"
    exit 1
fi
 
# Read last known IP and last update timestamp
LAST_IP=$(cat "$LAST_IP_FILE" 2>/dev/null)
LAST_UPDATE_TIMESTAMP=$(cat "$LAST_UPDATE_TIMESTAMP_FILE" 2>/dev/null || echo 0)
 
# Calculate seconds for the force update threshold
FORCE_UPDATE_SECONDS=$((FORCE_UPDATE_DAYS * 24 * 60 * 60))
 
# --- डिसीजन Logic ---
UPDATE_REASON=""
 
if [ "$CURRENT_IP" != "$LAST_IP" ]; then
    UPDATE_REASON="IP address has changed to $CURRENT_IP."
elif [ $((CURRENT_TIMESTAMP - LAST_UPDATE_TIMESTAMP)) -gt "$FORCE_UPDATE_SECONDS" ]; then
    UPDATE_REASON="Forcing periodic update. Last update was more than $FORCE_UPDATE_DAYS days ago."
fi
 
 
# --- Action ---
if [ -n "$UPDATE_REASON" ]; then
    echo "$(date): Triggering update. Reason: $UPDATE_REASON" >> "$LOG_FILE"
 
    # Call the update API
    RESPONSE=$(curl -s -X POST "https://myaddr.tools/update" -d "key=${API_KEY}" -d "ip=self")
 
    # Check if the update was successful
    if [ "$RESPONSE" == "ok" ]; then
        echo "$(date): DNS update successful." >> "$LOG_FILE"
        # On success, save the new IP AND the current timestamp
        echo "$CURRENT_IP" > "$LAST_IP_FILE"
        echo "$CURRENT_TIMESTAMP" > "$LAST_UPDATE_TIMESTAMP_FILE"
    else
        echo "$(date): DNS update FAILED. API Response: $RESPONSE" >> "$LOG_FILE"
        exit 1
    fi
else
    echo "$(date): IP address ($CURRENT_IP) is unchanged and within the time limit. No update needed." >> "$LOG_FILE"
fi
 
exit 0

Using systemd to manage repeated tasks instead of cron:

/etc/systemd/system/update-dns.service

[Unit]
Description=Update myaddr.tools DNS Record
 
[Service]
Type=oneshot
ExecStart=/bin/bash /path/to/your/update_dns.sh

/etc/systemd/system/update-dns.timer

[Unit]
Description=Run DNS update check periodically
 
[Timer]
OnBootSec=2min
OnUnitActiveSec=15min
Unit=update-dns.service
 
[Install]
WantedBy=timers.target