Find the answer to your Linux question:
Results 1 to 4 of 4
I have a process that does a ping to a server before it transmits backup data. This tells me if the server is up or down. I know that if ...
  1. #1
    Linux Newbie
    Join Date
    Jun 2006
    Posts
    139

    a question on return codes

    I have a process that does a ping to a server before it transmits backup data.
    This tells me if the server is up or down.
    I know that if I code something and it is good I get a 0 and if bad I get a 1.
    Now for my question:

    The ping is going to return something either good responses(response times) or a response that it timed out.
    Is there a way that if the ping returns with times(good) then the ftp world start and if it fails then the ftp would not proceed.
    Any ideas
    thanks
    mace

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Try this:

    Code:
    ping -c 1 <IP_address>
    if [ $? -eq 0 ]
       then ftp <whatever...>
       else echo "Server did no respond"
       fi

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I think that lomcevak has misunderstood.

    So you are saying that you will get some specific exit code if it is bad, and anything else means it is good. In that case, basically do the opposite of what he said:
    Code:
    ping IP_ADDRESS
    ping_return=$?
    
    if [ "$ping_return" -neq "$bad_return" ]; then
        # do your FTP stuff
    else
        # ping returned bad response
    fi
    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    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.

    -- excerpt from man ping
    So I think lomcevak's response is correct -- "0" is OK in scripting. Other values are not standardized, and there are at least 2 non-zero exit codes from ping. The deadline or timeout may be useful to specify, for example:
    Code:
    ping -c 1 -W 1 microsoft.com
    returns with exit code 1 after 1 second. Otherwise, the default seems to be 10 seconds.

    You may wish to discard the ping printable output, but that's something you can do once you get your code running correctly ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...