How to exit the interactive mode for yarn top command from within the bash script

 ̄綄美尐妖づ 提交于 2020-06-29 05:51:20

问题


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 Ctrl+C or quit signal/command manually.

Is there any way through which after running the above command and redirecting the output to test.txt the script should move to the 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


回答1:


Classic top can be run in non-interactive mode by top -n 1 Unfortunately I can't find documentation for yarn top

or you can try run command in background (&), save its pid, wait for data and kill it

ssh -oStrictHostKeyChecking=no -i <key> username@hostname "yarn top" | head -5 | grep -w 'Applications' | awk '{print "Pending_apps" "\t" $7}' >> test.txt & PID=$!; sleep 1; kill -s SIGINT $PID

hopefully ssh transfer SIGINT successfuly




回答2:


You can try to emulate the console by piping the output of echo like this:

(sleep $SLEEP_TIME; echo -e "q\n\n") | yarn top > yarn-top.log

It means: "run yarn top, and in parallel, wait SLEEP_TIME and then output q and two new lines".

As far as SLEEP_TIME is less than the default refresh delay on yarn top (3 seconds by default), you will get one screen update in yarn-top.log. If you set SLEEP_TIME greater than the refresh delay, you will get as many updates as it has time to run during the SLEEP_TIME.

You need echo -e to escape the newline chars. You need them because yarn top is implemented in java. See yarn top --help:

  1. Since the tool is implemented in Java, you must hit Enter for key presses to be processed.


来源:https://stackoverflow.com/questions/51729213/how-to-exit-the-interactive-mode-for-yarn-top-command-from-within-the-bash-scrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!