[英]Kill command line process and restart if STDOUT changes to what I'm looking for?
I have a question - I'm running a process from the command line that has some problem and poops out every few hours or so.我有一个问题 - 我正在从命令行运行一个有问题的进程,每隔几个小时左右就会出现一次。 While I'm looking into the issue, I'd like to spawn the process from something that monitors STOUT for certain string/regex and kills and restarts the process if it outputs something that indicates that it's broken.
在我研究这个问题时,我想从监视 STOUT 的某些字符串/正则表达式的东西中生成该进程,如果它输出表明它已损坏的内容,则终止并重新启动该进程。
I know I could do this the vanilla way by rolling my own Python/Ruby script but I was wondering if there's any nifty tools I can use to make this a bit cleaner?我知道我可以通过滚动我自己的 Python/Ruby 脚本来以普通的方式做到这一点,但我想知道是否有任何漂亮的工具可以用来让它更干净一点? This is on Windows but I have cygwin in case the answer involves a unix command line process.
这是在 Windows 上,但我有 cygwin,以防答案涉及 unix 命令行过程。
program |程序 | grep CRASH_STRING |
grep CRASH_STRING | xargs -l 1 sh -c 'killall program && program'
xargs -l 1 sh -c 'killall 程序 && 程序'
Well, that will do it once.好吧,那会做一次。 I'm not sure how to make that work in a loop.
我不确定如何让它循环工作。 I thought about it some more, and it can probably be done by redirecting stdout to a named pipe.
我想了更多,可能可以通过将标准输出重定向到名为 pipe 来完成。 But the shell script will probably end up more unwieldy than writing a watchdog in a scripting language.
但是 shell 脚本最终可能会比用脚本语言编写看门狗更笨拙。
But the idea with a pipe is something like this:但是 pipe 的想法是这样的:
mkfifo /tmp/fifo
program > /tmp/fifo&
while :
do
grep "CRASH_STRING" /tmp/fifo | xargs -l 1 sh -c 'killall program && program > /tmp/fifo&'
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.