Unix – Exit status of ping command

unix

My Perl script gets stuck with an exit status when trying to use the ping command.

Best Solution

According to this website:

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

To list the results:

  • Success: code 0
  • No reply: code 1
  • Other errors: code 2

Note that the page I link to says "Linux/Unix ping command", but other systems, or perhaps even variants of Linux and Unix, might vary this value.

If possible, I would test on the system in question to make sure you have the right ones.