Environment: armv7l GNU/Linux Debian Jessie, GNU bash, v4.3.30(1)-release (arm-unknown-linux-gnueabihf).

  • do_stuff: a long running process which generates output to STDOUT
  • compare_output.pl: a perl script which does things based on the output.

I want to be able to restart the compare_output.pl in case the process compare_output.pl breaks (dies). Even a simple echo after the while loop, (and after the compare_output.pl), was unsuccessful.

Sadly I lack the idea how to actually do this in bash. Bonus for retaining the Cache/Puffer of the do_stuff process.

while true;
  do_stuff (which generates lot of output linewise to STDOUT)
done | compare_output.pl 
#then something like this should happen, while retaining 
#the while loop and being able to "reattach" to the pipe.
"compare_output died? restart it "

Sometimes compare_output.pl breaks, (dies actually), which loses the whole pipe input. (Needless to say compare_output.pl should not break, but that is another problem for another day).

When compare_output breaks I want to be able to reuse the while loop, to be able to reattach to the pipe output. Losing one or a few lines is acceptable.

I tried using command substitution instead of a pipe:

while true;
  do_stuff (which generates lot of output linewise)
done < <( compare_output.pl )
# then something like this should happen, while retaining
# the while loop and being able to "reattach" to the pipe.
compare_output died? restart it "

restart_while_loop in case compare_output.pl fails (dies)

read and adapted from Bash: cannot break out of piped "while read" loop; process substitution works

This other than obstructing output seems to not resolve my issue that I want to act after compare_output.pl dies.

When I did CTRL-C then my echo test gets executed, which i did instead of the compare_output.pl restart test.

Otherwise do_stuff still runs for a long time.

Other than that I did read about piping and process substitution, lots of questions here but I seem to lack the right keyword to search for.

I know of Why is using a shell loop to process text considered bad practice? and understand that the way I do it is bad practice, but it's the situation I'm in which I currently cannot change.

So how can I get something to happen as soon as possible after compare_output.pl dies? (process stops running, no ps entry)

I am open for a full rewrite of the loop if that should be the only option to get this actually working. Currently I do not want to use other scripting languages, but if necessary I (somehow) could rewrite this in perl but would like not to.

  • It's not clear what this code is meant to do. After compare_output.pl dies, is the goal to run echo test or restart the while loop?
    – agc
    Apr 19, 2018 at 19:01
  • the plan is to restart the loop, sorry, i will edit this, i was under the impression that this are basically 2 issues i am having which i wanted to split down to not ask 2 questions in one. Apr 20, 2018 at 8:24
  • It seems to me you want to restart compare_output.pl if it breaks, and not the loop (which can be allowed to continue) ... which is what Jeff's answer suggests!
    – muru
    Apr 20, 2018 at 8:36
  • after further evaluation you are right muru that the question suggests the while loop restart, while Jeff is actually more of what the code really should do. I will reword the whole question to actually show that the compare_output.pl should restart, not the whole "while do_stuff loop" Apr 20, 2018 at 8:44
  • Re "more of what the code really should do": please be more specific, does Jeff Schaller's answer solve the problem or not?
    – agc
    Apr 20, 2018 at 14:49

1 Answer 1


Pipe the first loop into another loop:

while :; do ./do_stuff ; done | 
  while :; do ./compare_output.pl ; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .