# Set vmid and target IP vmid=101 target=192.168.1.1
# Set ping timeout to 3 seconds timeout=3
# Set maximum number of pings to 2 max_pings=2
# Set the interval between pings to 15 seconds interval=15
whiletrue; do # Set counter to 0 counter=0
# Loop until the counter exceeds the max pings while [ $counter -lt $max_pings ]; do # Ping the target and increment the counter if ! ping -c 1 -W $timeout$target &> /dev/null; then ((counter++)) else # Reset the counter if the ping is successful counter=0 fi
# Sleep for the interval before pinging again sleep$interval done
# If the loop finishes, that means that ping failed max_pings times # in a row, so we can check for the lock file and restart the virtual machine if [ -f "/var/lock/qemu-server/lock-${vmid}.conf" ]; then rm"/var/lock/qemu-server/lock-${vmid}.conf" fi qm stop $vmid sleep 15 qm start $vmid sleep 60
# Record the restart time in the log file echo"Virtual machine restarted at $(date)" >> /var/log/vmrestart.log done