开发者

Kill command line process and restart if STDOUT changes to what I'm looking for?

开发者 https://www.devze.com 2023-03-12 04:14 出处:网络
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 somet

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.

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 t开发者_JS百科o make this a bit cleaner? This is on Windows but I have cygwin in case the answer involves a unix command line process.


program | grep CRASH_STRING | xargs -l 1 sh -c 'killall program && program'

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. But the shell script will probably end up more unwieldy than writing a watchdog in a scripting language.

But the idea with a pipe is something like this:

mkfifo /tmp/fifo
program > /tmp/fifo&
while :
do
grep "CRASH_STRING" /tmp/fifo | xargs -l 1 sh -c 'killall program && program > /tmp/fifo&'
done
0

精彩评论

暂无评论...
验证码 换一张
取 消