I have a bash script which is on serverA. This script will ssh to serverB and runs yarn top command, pulls the metrics and put into the file(test.txt) on serverA. Below is the command which I am using:

ssh -oStrictHostKeyChecking=no -i <key> username@hostname "yarn top" | head -5 | grep -w 'Applications' | awk '{print "Pending_apps" "\t" $7}' >> test.txt

So here the problem is that after my script runs above command it goes into interactive mode and it only exits when I give crtl+c or quit signal/command manually.

Is there anyway through which after running the above command and redirecting the output to test.txt the script should move to next command. That is after running the above command on the command prompt it should redirect output to a test.txt file and then should return back to command prompt/terminal

  • How much output should it produce, or how long should it wait, before moving on? Do you want to interrupt it with control-C, or send a "q", or ?? – Jeff Schaller Aug 14 '18 at 10:43
  • The above command print's the output as "Pending_apps 3" in test.txt file, but then after it is not returning to the command prompt and the screen stays in the interactive mode. I would like to exit the interactive mode once the output is printed in test.txt file i.e once the command is successfully run I want it to return back to command prompt. I want to send a quit signal once the command is successful. When I press ctrl+c after running the above command for like 10secs then I could see the required output in test.txt file. – MichealMills Aug 14 '18 at 11:08

One simple suggestion would be to break the pipeline when you have what you want:

ssh -oStrictHostKeyChecking=no -i <key> username@hostname "yarn top" | 
  head -5 | 
  grep -w 'Applications' | 
  awk '{print "Pending_apps" "\t" $7; exit}' >> test.txt

This tells awk to exit after receiving the first line of input (since there are no conditions for the code block).

  • I have used exit in the awk but still its not working. I ran the above ssh command on the command line with exit in awk, but it is still unable to quit the interactive mode and the control is not returing back to command prompt. It is not working from the inside the script as well. – MichealMills Aug 16 '18 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.