fix: add check_internet.sh

This commit is contained in:
Denis Evers 2023-06-24 10:42:28 +08:00
parent beea227cb3
commit 3ae2a50e93
Signed by: denis-ev
GPG Key ID: 10BFC1EB323A6CA8
1 changed files with 53 additions and 0 deletions

53
check_internet.sh Executable file
View File

@ -0,0 +1,53 @@
#!/bin/bash
# Define the file path where the indicator file will be stored
indicator_file="./no-internet.txt"
# URL to ping when internet connection is available
ping_url="https://healthchecks.io/ping/{uuid}"
# Function to check internet connection
check_internet_connection() {
if ping -q -c 1 -W 1 cloudflare.com >/dev/null 2>&1; then
return 0 # Internet connection is available
else
return 1 # Internet connection is not available
fi
}
# Function to perform curl operation with error handling
perform_curl() {
if curl -fsS -m 10 --retry 5 -o /dev/null "$1"; then
echo "Curl to $1 was successful."
else
echo "Curl to $1 failed with error: $?"
fi
}
# Check internet connection
if check_internet_connection; then
echo "Internet connection is available."
# Ping the URL
echo "Pinging $ping_url"
perform_curl "$ping_url"
# Delete the indicator file if it exists
if [ -f "$indicator_file" ]; then
echo "Deleting the indicator file."
rm -f "$indicator_file" 2>/dev/null || echo "Error deleting $indicator_file"
fi
else
echo "No internet connection."
# Check if the indicator file exists
if [ -f "$indicator_file" ]; then
echo "Indicator file found. Waiting for 6 hours before rebooting..."
sleep 6h
echo "Rebooting now..."
sudo reboot
else
echo "Creating the indicator file and rebooting immediately."
touch "$indicator_file" 2>/dev/null || echo "Error creating $indicator_file"
echo "Rebooting now..."
sudo reboot
fi
fi