Skip to content

Instantly share code, notes, and snippets.

@TwitchBronBron
Created September 27, 2023 02:11
Show Gist options
  • Save TwitchBronBron/0d2d97dd3e8881708f390632db5291e7 to your computer and use it in GitHub Desktop.
Save TwitchBronBron/0d2d97dd3e8881708f390632db5291e7 to your computer and use it in GitHub Desktop.
reboot computer if network is unavailable
# Define the log file path
log_file="$(dirname "$0")/reboot.sh.log"
# Define a function to log messages with date stamps
log_message() {
local message="$1"
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Use tee to echo the message to both the console and the log file
echo "[$timestamp] $message" | tee -a "$log_file"
}
if /bin/ping -q -c 1 192.168.1.1 &> /dev/null; then
log_message "Network is available"
# If the network is available, delete the counter file
rm -f "$counter_file"
else
# Check if the counter file exists, and if not, create it with an initial value of 0
counter_file="$(dirname "$0")/reboot.sh.counter"
if [ ! -f "$counter_file" ]; then
echo "0" > "$counter_file"
fi
# Read the current value of the counter from the file
counter=$(<"$counter_file")
# Check if the counter is less than 5
if [ "$counter" -lt 5 ]; then
log_message "Network is not available"
# Increment the counter and save it to the file
((counter++))
echo "$counter" > "$counter_file"
shutdown -r now
else
log_message "The else block has been run 5 times, so it won't run again."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment