how to restart consumer process using producer process in pipeline

64 views Asked by At

I have this exactly:

set -eo pipefail;

while true; do
    echo '(re)starting server...'
    (
    while read line; do
    if test "$line" == 'rs'; then
       echo 'restarting...' > /dev/stderr
       exit 0
    fi
    done
    ) |  go run src/main/main.go
done;

but when exit 0 is called in the producer, the go consumer doesn't die. Is there a way to capture a signal in the consumer?

1

There are 1 answers

3
yi yang On

(X) and A|B, the X, A, and B commands are executed in the newly created subshell, and the exit command exits only the subshell in which the command was executed, so the pipe character and the corresponding go run commands will still be executed normally. So you can have the go run and exit commands run in the same shell process, and then control when the go run is executed and when it exits with conditional statements. try the following code:

while true; do

    echo '(re)starting server...'
    while read line; do
        if test "$line" == 'rs'; then
           echo 'restarting...' > /dev/stderr
           exit 0
        fi
    done
    go run src/main/main.go
done;