0

In the below code, while loop is executing only one time whereas there are entry in crawler.cfg file. when I am commmenting VAR=$(ssh ${HOSTS} ps ax | grep -v grep | grep $SERVICE|wc -l) line in code then it is looping three times and working fine...

So, What is problem in this line?

Please assist.

while  read crawler_info
do
    PATH_OF_SERVER="/home/Crawler/"
    THREAD_SCRIPT=crawler18.sh
    HOSTS=$(echo $crawler_info|cut -d'|' -f1)
    SERVICE="java"

    VAR=$(ssh ${HOSTS} ps ax | grep -v grep | grep $SERVICE|wc -l)

    if [ $VAR -ne 0 ]
    then
        echo "$SERVICE service running, everything is fine on  ${HOSTS} node"
    else
        echo "java services is not running on ${HOSTS} node. process is triggering java services"
        ssh ${HOSTS}  ${PATH_OF_SERVER}${THREAD_SCRIPT}  > /dev/null 2> /dev/null&

        if [ $? -eq 0 ]
            then
                    echo "java services triggered successfully on ${HOSTS} node"
            else
                    echo "process is unable to trigger the java services on ${HOSTS} node"
            fi
    fi

done < crawler.cfg

crawler.cfg

n0007
n00011
n0000023
  • This is really, really not the right way to monitor and restart services. Consider using a tool built for the job -- runit, daemontools, upstart, systemd, or one of many others. – Charles Duffy Mar 03 '15 at 17:05
  • ...any of the above will respond immediately on service death (instead of waiting for an external polling process), and will require no extra resources to do the check (as they use `waitpid()` from a parent process to receive immediate notice when an exit occurs). – Charles Duffy Mar 03 '15 at 17:07

1 Answers1

0

This is rather a FAQ.

The easiest way to solve it is to use a FD other than 0:

while read -u 3 ...
done 3<crawler.cfg

Alternately, redirect ssh's stdin from /dev/null in both invocations:

VAR=$(ssh ${HOSTS} ps ax </dev/null | grep -v grep | grep $SERVICE|wc -l)

ssh ${HOSTS}  ${PATH_OF_SERVER}${THREAD_SCRIPT} </dev/null >/dev/null 2>&1
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441