LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2012, 03:25 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
Some help needed with my init script


I've got a script which continually pings two IP addresses, in the event of either failing, a piece of software is restarted and details are logged. This is meant to run as a background process and only allow single instances of the ping one liner'. Specifically this does not work - can someone help me troubleshoot - thanks.

Code:
ROUTER="10.161.1.1 10.161.1.2"
#RESTART_COMMAND=`/etc/init.d/opennhrp restart`
LOG=/var/log/messages
PING_STATE=0
start() {
                PING_ON=`pgrep -lf /bin/ping | wc -l`
                if [ $PING_ON -eq 1 ]; then
                        echo "Already running, run restart to restart the process."
                        exit 1
                fi
                while [ $PING_ON -eq 0 ]
                do
                        for IP in $ROUTER;
                                do
                                        nohup /bin/ping -c 10 $IP &> /dev/null &
                                        if [ $? -ne 0 ]; then
                                                let PING_STATE=PING_STATE+1
                                                FAILED_SERVER=$IP
                                        fi
                                done
                                        if [ $PING_STATE -ge 2 ]; then
                                                echo `date "+%b  %e %T"` `hostname -s` "Tunnel to $FAILED_SERVER went down, opennhrp restarting." >> $LOG
                                                /etc/init.d/opennhrp restart
                                                PING_STATE=0
                                        fi
                        pkill -f /bin/ping
                done
}

stop () {
        STOP=0
        for i in `pgrep -lf /bin/ping | awk '{print $1}'`
                do
                        kill -9 $i > /dev/null 2>&1
                        pkill -f /bin/ping > /dev/null 2>&1
                        stop
                        let STOP=STOP+1
                        if [ $STOP -eq 1 ]; then
                                echo Stoppping ping requests...
                        fi
                done
        return 0
}

case "$1" in
        start)
        start
        ;;

        stop)
        stop
        ;;

        restart|reload|force-reload)
        stop
        start
        ;;

        *)
        echo "Usage: /etc/init.d/pingcheck {start|stop|reload|force-reload|restart}"
        exit 1
esac

exit 0
 
Old 05-04-2012, 06:12 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What are the symptoms of "does not work"?
 
Old 05-04-2012, 06:16 AM   #3
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
When I exit the script running from ssh, the ping stops. The behaviour is not the same when run via init; not sure if this is a suitable way to run it...
 
Old 05-04-2012, 07:37 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
There are two issues I can see:
  1. You do the pinging on the background.

    Why on earth would you do that? The proper way to do heartbeat pinging is by doing only one, with a timeout:
    Code:
    ping -c 1 -w seconds host
    The output from the command is not important, as it will return nonzero/failure exit status if the host does not respond within five seconds. Typically, you'd simply run that command in a loop, sleeping a few seconds between checks. A simple if ! ping -c 1 -w seconds host &>/dev/null ; then ... fi is enough to catch the case when the host does not answer to a ping. (You might wish to have your script first escalate to "alert" mode, perhaps doing more pings, before concluding it is not accessible.)
  2. You may not be detaching the daemon from your session.

    Whenever a login session (be it console or SSH or X11) ends, a TERM signal is sent to the entire session, killing all processes still alive in the session.

    To properly daemonize a command, I recommend using
    Code:
    ( setsid command args.. </dev/null >/dev/null 2>/dev/null & )
    Because the command is in parentheses, it is run in a subshell. Because of the ampersand, the command is run in the background of the subshell. That alone is enough to keep it running, as the backgrounded command is reparented to init when the subshell exits. Certain signals are delivered via the tty; redirecting standard input, output and error to /dev/null severs that connection. Finally, setsid starts a new session for the command to run in. In all, this completely severs the connection between the starting shell and the command itself.
 
Old 05-04-2012, 08:00 AM   #5
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
The ping actually keeps the link up. I'll give your suggestions a try though and tell you if it still works, thanks
 
Old 05-04-2012, 08:09 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by genderbender View Post
The ping actually keeps the link up.
That does not matter at all. There is nothing wrong in pinging at a regular interval; I only mean that running the ping command on the background is the wrong way to do it.

Like I wrote above, your script should simply regularly call ping -c 1 -w seconds host in a loop, not keep pinging on the background. Whenever the command fails, restart opennhrp, then re-start the pinging loop.

The optimal delay (sleep in the loop) depends on what you consider a failure -- a single ping might get lost now and then, so maybe three or four successive failures constitute a downed link? --, and the maximum interval between pings when keeping the connection alive. Remember that in your script, the true maximum ping interval is the ping timeout PLUS the sleep duration.

Do you want to see a skeleton Bash script I'd personally start with?
 
Old 05-04-2012, 08:16 AM   #7
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
OK, it seems to instantly terminate when I run this and then enter a loop; output is as follows (infinitely):

Code:
/etc/init.d/pingcheck: line 5: 16133 Terminated              setsid nohup /bin/ping -c 1 -w 5 $IP < /dev/null > /dev/null 2> /dev/null
/etc/init.d/pingcheck: line 5: 16136 Terminated              setsid nohup /bin/ping -c 1 -w 5 $IP < /dev/null > /dev/null 2> /dev/null
etc
Obviously if I run this in parentheses no output is displayed...
 
Old 05-04-2012, 08:55 AM   #8
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Found potentially another problem, if the init script is started while opennhrp is being restarted (no pings running) then it will start an additional instance. I'm interested in your skeleton script, although don't want to have to rework my entire script... The basics are very similar, ping one host, then ping another if both fail restart something and try again after a short delay.
 
Old 05-04-2012, 09:35 AM   #9
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
First, you need to separate your init script and the actual daemon.

Assume you have /etc/pingcheck.conf with
Code:
# This is pingcheck configuration file,
# sourced by Bash.

# Hosts pinged.
ENDPOINTS="10.161.1.1 10.161.1.2"

# Command to run when ALL endpoints become unreachable.
COMMAND="/sbin/service opennhrp restart"

# Minimum interval between pings
INTERVAL=30

# Ping timeout (maximum ping interval = INTERVAL+TIMEOUT)
TIMEOUT=5

# Syslog facility and tag.
FACILITY=user
LOGTAG=pingcheck

# PID file location.
PIDFILE=/var/run/pingcheck.pid
The daemon could then be (untested!) say /usr/local/bin/pingcheck:
Code:
#!/bin/bash

# This script can only be run by root.
if [ "$(/usr/bin/id -u)" != "0" ]; then
    echo "This script can only be run by root." >&2
    exit 1
fi

# Source the configuration file.
. /etc/pingcheck.conf || exit $?

# Some of the commands yield unused output; discard it all.
exec </dev/null >/dev/null 2>/dev/null

while [ 1 ]; do

    ok=0
    count=0
    for IP in $ENDPOINTS ; do
        ping -c 1 -w $TIMEOUT $IP &>/dev/null && ok=$[ok + 1]
        count=$[count + 1]
    done

    # All pings successful?
    if [ $ok -eq $count ]; then
        sleep $INTERVAL
        continue
    fi

    # All pings failed?
    if [ $ok -eq 0 ]; then
        if $COMMAND ; then
            logger -i -p $FACILITY.notice -t $LOGTAG "$[count-ok] of $count endpoints unreachable; restart successful"
        else
            logger -i -p $FACILITY.notice -t $LOGTAG "$[count-ok] of $count endpoints unreachable; restart failed"
        fi
    fi

    # Sleep until next round.
    sleep $INTERVAL
done
This version uses logger to log to syslog, but you can obviously do whatever you prefer.

The "all pings successful" if clause is actually not needed. I included it to make it easier for you to edit or add conditions to suit your needs; this way the success case is explicitly handled correctly.

In the init script, use ps -o pid= -C pingcheck to get the PID of the running daemon. It will return nothing if the daemon is not running. To terminate the script, just send it a TERM signal; i.e.
Code:
pid=$(ps -o pid= -C pingcheck)
[ -n "$pid" ] && kill -TERM $pid
To test the script, use for example
Code:
sudo sh -c '( setsid /usr/local/bin/pingcheck </dev/null >/dev/null 2>/dev/null & )'
In the init script, you can start the daemon using
Code:
( setsid /usr/local/bin/pingcheck </dev/null >/dev/null 2>/dev/null & )
 
1 members found this post helpful.
Old 05-04-2012, 11:13 AM   #10
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Wow - thanks for your help I'll give it a shot.
 
Old 05-04-2012, 11:38 AM   #11
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by genderbender View Post
I'll give it a shot.
Okay, just let us know your results. (I should be able to fix any obvious problems in the script, if you find any.)

The init script needed to start, restart, stop, or query the status of the daemon is very simple, very similar to your original init script. If the daemon script works for you, then we can easily edit your init script to control the daemon script instead.
 
Old 05-08-2012, 04:17 AM   #12
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
This worked a treat, big thanks for your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Start-Up Script & Shutdown/Kill Script needed guggilamsandeep Red Hat 1 05-11-2011 08:58 AM
Init script in /etc/init.d does not start at boot tdnnash25 Linux - Server 4 12-18-2009 04:40 PM
needed bandwidthd init script in /etc/rc.d/ Teomari Slackware 4 03-30-2007 07:51 PM
Mailscanner init script needed dodgeboy Linux - Software 0 11-07-2005 10:04 PM
urgent help needed with init nmolinos Linux - General 2 11-26-2003 10:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration