keeping an occasional ping alive

Kevin Buettner kev@primenet.com
Thu, 6 Jul 2000 22:00:18 -0700


On Jul 6,  8:30pm, Craig White wrote:

> I have a client that has a problem with their ISDN and we want to keep the
> connection alive - so we would probably want to ping an internet address
> once every 30 seconds.
> 
> I read man ping and found that I can ping address -i30 -q but I want that
> process in the background so I get a shell prompt so I add an ampersand to
> the line to make it...
> 
> ping (address) -i30 -q &
> 
> but it still doesn't return to the shell prompt unless I hit return a second
> time - why? can anyone clue in the clueless?  Is this a reasonable way to do
> this?

Are you certain that you're not seeing a shell prompt?

E.g, when I try it, here's what I see:

    ocotillo:kev$ ping 192.168.2.1 -i30 -q &
    [1] 3192
    ocotillo:kev$ PING 192.168.2.1 (192.168.2.1) from 192.168.2.7 : 56(84) bytes of data.


Note that the prompt and the initial output from ping ended up on
the same line.  You can avoid even the initial output by doing the
following:

    ocotillo:kev$ ping 192.168.2.1 -i30 -q >/dev/null &
    [1] 3209
    ocotillo:kev$ 

The parent of the ping will be your shell process.  It may be
desirable to "daemonize" the ping process so that your shell
can exit without killing the ping.  You can do that as follows:

    ocotillo:kev$ ( ping 192.168.2.1 -i30 -q >/dev/null & ) &
    [1] 3222
    ocotillo:kev$ 
    [1]+  Done                    ( ping 192.168.2.1 -i30 -q >/dev/null & )

The ping hasn't really exited, only the subshell enclosed by parens has
exited.  If you grep for ping, it's still alive and kicking:

    ocotillo:kev$ ps axlww | grep ping
    100   204  3223     1   4   0  1224  472 skb_re S    pts/28     0:00 ping 192.168.2.1 -i30 -q

Note that the ping's parent pid is 1 which is the init process.  This is
normal for child processes who've lost their parent; init inherits them.
This is good because 1) it allows you to exit your shell without killing
the ping and 2) if the ping should exit for some reason, init will do
the wait() or waitpid() and prevent the process from becoming a zombie.
(Actually, the shell should do this for you too, but it possible to
come up with a pathological example in which a zombie is created.)

Kevin