Quit a running command based on output in bash

走远了吗. 提交于 2019-12-11 06:10:33

问题


I have a process which I start running using

$ somecommand build

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.

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?


回答1:


Using awk, with help from pkill:

somecommand build | awk '/\[TESTS_ENDED\]/ {system("pkill somecommand")}'
  • Assuming only one instance of somecommand is runnning

  • \[TESTS_ENDED\] matches [TESTS_ENDED] in the line

  • If matched, {system("pkill somecommand")} will be run i.e. awk will use system() function to run pkill somecommand i.e. somecommand will be killed by pkill



来源:https://stackoverflow.com/questions/40762233/quit-a-running-command-based-on-output-in-bash

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