Skip to content

Instantly share code, notes, and snippets.

@bulletinmybeard
Created October 3, 2024 14:23
Show Gist options
  • Save bulletinmybeard/3cd8c4cd49444b05aad7316ff7b12cb8 to your computer and use it in GitHub Desktop.
Save bulletinmybeard/3cd8c4cd49444b05aad7316ff7b12cb8 to your computer and use it in GitHub Desktop.
Ping Failure Monitor Command

Ping Failure Monitor Command

This command continuously pings google.com and logs any connection failures. It's useful for monitoring network connectivity and identifying intermittent issues.

while true; do
  if ! ping -c 1 -W 5 google.com &> /dev/null; then
    echo "$(date): Connection to google.com failed"
  fi
  sleep 1
done

Explanation of Ping Command Arguments

  1. if !: Starts an if statement that executes the following block if the command returns a non-zero exit status (i.e., if it fails).

  2. ping: The command used to test the reachability of a host on an IP network.

  3. -c 1:

    • Argument: -c
    • Value: 1
    • Meaning: Tells ping to send only one packet. The 'c' stands for "count".
  4. -W 5:

    • Argument: -W
    • Value: 5
    • Meaning: Sets the timeout to 5 seconds. The uppercase 'W' stands for "timeout" and waits longer than the lowercase 'w'.
  5. google.com: The target host to ping.

  6. &> /dev/null: Redirects both standard output and standard error to /dev/null, effectively silencing all output from the ping command.

  7. ;: Separates the ping command from the then keyword that follows.

  8. then: Indicates the beginning of the code block to be executed if the ping fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment