Skip to content

Instantly share code, notes, and snippets.

@klevo
Created February 15, 2024 15:51
Show Gist options
  • Save klevo/e9e70a795e921062bc5eaa4b519570f7 to your computer and use it in GitHub Desktop.
Save klevo/e9e70a795e921062bc5eaa4b519570f7 to your computer and use it in GitHub Desktop.
Restart AWS SSM service after network goes down and then up again
#!/bin/bash
host_to_ping="10.20.4.201"
services_to_restart=("amazon-ssm-agent")
# Check network status before starting the loop
if ping -c 1 -W 1 $host_to_ping > /dev/null; then
echo "Network is up. Script is running in monitoring mode."
else
echo "Network is down. Restarting services..."
for service in "${services_to_restart[@]}"; do
systemctl restart $service
done
fi
# Monitor network and restart services as needed
while true; do
if ping -c 1 -W 1 $host_to_ping > /dev/null; then
echo "Network is up. Waiting for connection..."
else
echo "Network is down. Restarting services..."
for service in "${services_to_restart[@]}"; do
systemctl restart $service
done
fi
sleep 3 # Adjust the sleep time as needed
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment