开发者

Bash Script Monitor Program for Specific Output

开发者 https://www.devze.com 2023-04-02 16:54 出处:网络
So this is probably an easy question, but I am not much of a bash programmer and I haven\'t been able to figure this out.

So this is probably an easy question, but I am not much of a bash programmer and I haven't been able to figure this out.

We have a closed source program that calls a subprogram which runs until it exits, at which point the program will call the subprogram again. This repeats indefinitely.

Unfortunately the main program will sometimes spontaneously (and repeatedly) fail to call the subprogram after a random period of time. The eventual solution is to contact the original developers to get support, but in the meantime we need a quick hotfix for the issue.

I'm trying to write a bash script that will monitor the output of the program and when it sees a specific string, it will restart the machine (the program will run again automatically on boot). The bash script needs to pass all standard output through to the screen up until it sees the开发者_JAVA百科 specific string. The program also needs to continue to handle user input.

I have tried the following with limited success:

./program1 | ./watcher.sh

watcher.sh is basically just the following:

while read line; do
    echo $line
    if [$line == "some string"]
    then
        #the reboot script works fine
        ./reboot.sh 
    fi
done

This seems to work OK, but leading whitespace is stripped on the echo statement, and the echo output hangs in the middle until subprogram exits, at which point the rest of the output is printed to the screen. Is there a better way to accomplish what I need to do?

Thanks in advance.


I would do something along the lines of:

stdbuf -o0 ./program1 | grep --line-buffered "some string" | (read && reboot)


  1. you need to quote your $line variable, i.e. "$line" for all references *(except the read line bit).

  2. Your program1 is probably the source of the 'paused' data. It needs to flush its output buffer. You probably don't have control of that, so

    a. check if your system has unbuffer command available. If so try unbuffer cmd1 | watcher You may have to experiment with which cmd you wrap unbuffer with, maybe you whill have to do cmd1 | unbuffer watcher.

    b. OR you can try wrapping watcher as a process-group, (I think that is the right terminology), i.e.

    ./program1 | { ./watcher.sh ; printf "\n" ; }

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.


use read's $REPLY variable, also I'd suggest using printf instead of echo

while read; do
    printf "%s\n" "$REPLY"
    # '[[' is Bash, quotes are not necessary 
    # use '[ "$REPLY" == "some string" ]' if in another shell
    if [[ $REPLY == "some string" ]] 
    then
        #the reboot script works fine
        ./reboot.sh 
    fi
done
0

精彩评论

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

关注公众号