0

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
stack
  • 821
  • 1
  • 15
  • 28

1 Answers1

1
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
Kenster
  • 23,465
  • 21
  • 80
  • 106
  • Duplicates (and this is _very_ much a duplicate -- it's not just all over our knowledge base, but is also [BashFAQ #89](https://mywiki.wooledge.org/BashFAQ/089)) should be closed, not answered; see the bullet point regarding questions that "have already been asked and answered many times before" in the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Jul 02 '20 at 16:12
  • @CharlesDuffy I understand the concept of closing questions, and I know this is a faq. When I saw this question it had gone 7 hours without any close votes. My impression has been that questions like this are hard to close because each version of the question is unique in its own way. – Kenster Jul 02 '20 at 16:23
  • Ahh. That's a place where the gold-badge dupehammer comes in handy. That said, if something is unique only for reasons that aren't really distinguishing (such that an answer to one will answer the others; and the OP could/should have simplified the question when asking it to be more minimal in a way that would have removed those distinctions), they don't really cut against. – Charles Duffy Jul 02 '20 at 16:25
  • I have another script with while loop which has ssh , and it runns good. the jobIdUser has content like: r01n01 ccc-xhiqcc. see my update – stack Jul 03 '20 at 00:04
  • @Kenster I have tested as you said, and it works OK now. Thank you very much – stack Jul 03 '20 at 00:14