Created
February 15, 2024 15:51
-
-
Save klevo/e9e70a795e921062bc5eaa4b519570f7 to your computer and use it in GitHub Desktop.
Restart AWS SSM service after network goes down and then up again
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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