问题
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
somecommandis runnning\[TESTS_ENDED\]matches[TESTS_ENDED]in the lineIf matched,
{system("pkill somecommand")}will be run i.e.awkwill usesystem()function to runpkill somecommandi.e.somecommandwill be killed bypkill
来源:https://stackoverflow.com/questions/40762233/quit-a-running-command-based-on-output-in-bash