Use edgerouter_wait_start_vms.sh on Unraid as part of a broader startup automation system

My EdgeRouter ping and start up script for VM optimized.

#!/bin/bash
#
# edgerouter_wait_start_vms.sh
#
# Description: Waits for edgerouter to become available before starting libvirt VMs.
#             Useful for automated VM startup after host/network reboots.
# 
# Usage: ./edgerouter_wait_start_vms.sh
#
#
# Dependencies: 
#   - libvirt-clients (virsh)
#   - ping
#

EDGEROUTER_IP="192.168.1.1"  # EdgeRouter gateway IP
VM_NAME="VMName1"         # VM to start
MAX_ATTEMPTS=60          # Maximum number of attempts (10 minutes with 10s sleep)
SLEEP_INTERVAL=10        # Seconds to wait between attempts
PING_COUNT=2            # Number of successful pings required
PING_TIMEOUT=2          # Ping timeout in seconds

# Function to check if EdgeRouter is responding
check_edgerouter() {
    ping -c ${PING_COUNT} -W ${PING_TIMEOUT} ${EDGEROUTER_IP} >/dev/null 2>&1
    return $?
}

# Function to check if virsh is available
check_virsh() {
    if ! command -v virsh >/dev/null 2>&1; then
        echo "Error: virsh command not found. Please install libvirt-clients package."
        exit 1
    fi
}

# Main script
echo "Waiting for EdgeRouter (${EDGEROUTER_IP}) to become available..."
attempt=0

check_virsh

while ! check_edgerouter; do
    printf "."
    attempt=$((attempt + 1))
    
    if [ ${attempt} -ge ${MAX_ATTEMPTS} ]; then
        echo -e "\nError: Timeout waiting for EdgeRouter to respond after $((MAX_ATTEMPTS * SLEEP_INTERVAL)) seconds"
        exit 1
    fi
    
    sleep ${SLEEP_INTERVAL}
done

echo -e "\nedgerouter is online!"

# Start the VM
echo "Starting VM: ${VM_NAME}"
if virsh start "${VM_NAME}"; then
    echo "Successfully started ${VM_NAME}"
else
    echo "Error: Failed to start ${VM_NAME}"
    exit 1
fi

exit 0

Key improvements made for my operations:

  1. Added proper variable declarations at the top for easy configuration
  2. Added configurable timeout and retry settings
  3. Improved error handling and status messages
  4. Added check for virsh availability
  5. Added more robust ping check with configurable count and timeout
  6. Added proper exit codes for error handling
  7. Added feedback for VM startup success/failure
  8. Made the script more maintainable with comments and consistent formatting
  9. Added protection against unbound variables with proper quoting
  10. Added proper stdout/stderr redirection

Optional improvements you might want to consider for your additional VM’s:

  1. If you want to start multiple VMs with delays, you could add an array:
# Add this to the variables section:
VM_NAMES=("VM1" "VM2" "VM3")
VM_DELAYS=(0 30 60)  # Delays in seconds before starting each VM

# Replace the VM start section with:
for i in "${!VM_NAMES[@]}"; do
    if [ "${VM_DELAYS[$i]}" -gt 0 ]; then
        echo "Waiting ${VM_DELAYS[$i]} seconds before starting ${VM_NAMES[$i]}"
        sleep "${VM_DELAYS[$i]}"
    fi
    
    echo "Starting VM: ${VM_NAMES[$i]}"
    if virsh start "${VM_NAMES[$i]}"; then
        echo "Successfully started ${VM_NAMES[$i]}"
    else
        echo "Error: Failed to start ${VM_NAMES[$i]}"
        exit 1
    fi
done
  1. You might want to add a logger:
# Add to the top after shebang:
exec 1> >(logger -s -t "$(basename "$0")") 2>&1
  1. You could add a trap for cleanup:
# Add near the top:
trap 'echo "Script interrupted. Exiting..."; exit 1' INT TERM