根据bash中的输出退出正在运行的命令 [英] Quit a running command based on output in bash
问题描述
我有一个开始使用的进程
I have a process which I start running using
$ somecommand构建
它基本上是在Android模拟器上安装应用程序,然后启动模拟器.这个过程一直持续到我手动将其杀死为止.
It basically installs an app on an Android emulator and then starts up the emulator. This process keeps running forever until i manually kill it.
此命令在运行应用程序时也会向终端输出一堆调试信息.我想在此连续输出中的某处在终端上输出字符串 [TESTS_ENDED]
时终止该命令.
This command outputs a bunch of debug info into the terminal, also while running the app. I'd like to kill the command when somewhere in this continuous output, the string [TESTS_ENDED]
has been outputted on the terminal.
有任何关于如何做到这一点的人吗?
Any oneliner on how to do this?
推荐答案
在 pkill
的帮助下,使用 awk
:
somecommand build | awk '/\[TESTS_ENDED\]/ {system("pkill somecommand")}'
-
假设正在运行
somecommand
的一个实例\ [TESTS_ENDED \]
与该行中的[TESTS_ENDED]
匹配如果匹配,将运行
{system("pkill somecommand")}
,即awk
将使用system()
函数运行pkill somecommand
,即somecommand
将被pkill
If matched,
{system("pkill somecommand")}
will be run i.e.awk
will usesystem()
function to runpkill somecommand
i.e.somecommand
will be killed bypkill
这篇关于根据bash中的输出退出正在运行的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!