如何从bash脚本中退出yarn top命令的交互模式 [英] How to exit the interactive mode for yarn top command from within the bash script
问题描述
我在serverA上有一个bash脚本.该脚本将SSH到serverB并运行yarn top命令,提取度量并将其放入serverA的文件(test.txt)中.下面是我正在使用的命令:
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
所以这里的问题是我的脚本在上面的命令中运行后,它进入了交互模式,并且仅当我手动提供Ctrl + C或退出信号/命令时才退出.
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.
在运行上述命令并将输出重定向到test.txt之后,脚本是否可以通过任何方式移动到下一个命令.也就是说,在命令提示符下运行上述命令后,应将输出重定向到test.txt文件,然后应返回到命令提示符/终端
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
推荐答案
您可以通过管道化 echo
的输出来尝试模拟控制台,如下所示:
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
这意味着:运行纱顶,并并行,等待 SLEEP_TIME
,然后输出q和两条新线".
It means: "run yarn top, and in parallel, wait SLEEP_TIME
and then output q and two new lines".
只要 SLEEP_TIME
小于 yarn top
上的默认刷新延迟(默认为3秒),您将在 yarn-top.log
.如果将 SLEEP_TIME
设置为大于刷新延迟,则在 SLEEP_TIME
期间,您将获得尽可能多的更新.
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
.
您需要 echo -e
来换行符.您需要它们,因为 yarn top
是用Java实现的.请参见 yarn top --help
:
You need echo -e
to escape the newline chars. You need them because yarn top
is implemented in java. See yarn top --help
:
- 由于该工具是用Java实现的,因此必须按Enter键才能处理按键.
这篇关于如何从bash脚本中退出yarn top命令的交互模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!