虽然在使用ssh期间循环不起作用

时间:2015-03-03 17:02:28

标签: shell

在下面的代码中,while循环只执行一次,而crawler.cfg文件中有条目。 当我在代码中提交VAR=$(ssh ${HOSTS} ps ax | grep -v grep | grep $SERVICE|wc -l)行时,它循环三次并正常工作......

那么,这一行有什么问题?

请协助。

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

1 个答案:

答案 0 :(得分:0)

这是一个常见问题解答。

解决问题的最简单方法是使用0以外的FD:

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

或者,在两次调用中都从/dev/null重定向ssh的stdin:

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