简体   繁体   中英

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.

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? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM