Search code examples
shellopenssh

Why it is exit when ssh remote executed script in linux shell


In my project, i need to find the user processed on node.

I have a file: jodIdUser. The content in this file has two columns, like:

395163 chem-yupy
395164 chem-yupy
395165 phy-xiel
395710 mae-chent

Now i have a script appRecord.sh, and i have a whle loop in it. The while method code is like:

cat $workDir/jobIdUser | while read LINE
do
  jobUser=`echo $LINE | awk '{print $2}'`
  jobId=`echo $LINE | awk '{print $1}'`

  jobOnNodes=`/usr/bin/jobToNode $jobId | xargs`
  echo $timeStr" "$jobId" "$jobUser" "$jobOnNodes  >> $workDir/tmpRecFile

  #20200702, it is needed to find out user process on nodes at this time here
   designatedNode=`echo $jobOnNodes | awk '{print $NF}'`
   echo $jobOnNodes
   echo $designatedNode

   ssh $designatedNode sh $workDir/nodeProInfo.sh       ##Here code will exit while loop
   echo $timeStr" "$jobId" "$jobUser" "$jobOnNodes >> $workDir/$recordFile
 done

The code of nodeProInfo.sh is like:

#!/bin/bash
source /etc/profile
workDir=/work/ccse-xiezy/appRecord

hostName=`hostname`
jobInfo=`ps axo user:15,comm | grep -Ev "libstor|UID|ganglia|root|gdm|postfix|USER|rpc|polkitd|dbus|chrony|libstoragemgmt|para-test|ssh|ccse-x|lsf|lsbatch" | tail -n 1`

echo $hostName" "$jobInfo >> $workDir/proRes

Now I run the script sh appRecord.sh, it is wrong. It will exit when the first loop in while

[cc@login04 appRecord]$ sh appRecord.sh 
 r03n56 r04n09 r04n15
 r04n15
[cc@login04 appRecord]$

I don't know why it will exit when remote ssh node method, who can help me?

UPDATE:

I have another script which runns ok. the jobIdUser has content like:

   r01n23  xxx-ser
   r92n12  yyn-ser

and the while loop is:

cat $workDir/jobIdUser | while read LINE
do
     .............
     ssh $NODE pkill -u -9 $USER
     .............
done

Solution

  • cat $workDir/jobIdUser | while read LINE
    do
       ...
       ssh $designatedNode sh $workDir/nodeProInfo.sh       ##Here code will exit while loop
       ...
    done
    

    ssh (and every other command running inside the loop) inherits its standard input from the standard input of the while loop. By default, ssh reads its standard input and passes the data to the standard input of the remote process. This means that ssh is consuming the data being piped into the loop, preventing the while statement from reading it.

    You need to prevent ssh from reading its standard input. You can do this one of two ways. First of all, the -n flag prevents ssh from reading its standard input:

    ssh  -n $designatedNode sh $workDir/nodeProInfo.sh
    

    Or you can redirect ssh's standard input from another source:

    ssh $designatedNode sh $workDir/nodeProInfo.sh < /dev/null