Pipe from a while loop to a command but execute another command if pipe command fails

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












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







share|improve this question





















  • 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 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.
    – Dennis Nolte
    Apr 20 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 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"
    – Dennis Nolte
    Apr 20 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 at 14:49














up vote
-1
down vote

favorite












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







share|improve this question





















  • 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 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.
    – Dennis Nolte
    Apr 20 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 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"
    – Dennis Nolte
    Apr 20 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 at 14:49












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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







share|improve this question













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









share|improve this question












share|improve this question




share|improve this question








edited Apr 20 at 14:46









agc

4,0091935




4,0091935









asked Apr 19 at 16:28









Dennis Nolte

186112




186112











  • 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 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.
    – Dennis Nolte
    Apr 20 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 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"
    – Dennis Nolte
    Apr 20 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 at 14:49
















  • 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 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.
    – Dennis Nolte
    Apr 20 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 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"
    – Dennis Nolte
    Apr 20 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 at 14:49















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 at 19:01




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 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.
– Dennis Nolte
Apr 20 at 8:24




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.
– Dennis Nolte
Apr 20 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 at 8:36





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 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"
– Dennis Nolte
Apr 20 at 8:44





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"
– Dennis Nolte
Apr 20 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 at 14:49




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 at 14:49










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Pipe the first loop into another loop:



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





share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438772%2fpipe-from-a-while-loop-to-a-command-but-execute-another-command-if-pipe-command%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Pipe the first loop into another loop:



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





    share|improve this answer

























      up vote
      2
      down vote



      accepted










      Pipe the first loop into another loop:



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





      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Pipe the first loop into another loop:



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





        share|improve this answer













        Pipe the first loop into another loop:



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






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Apr 19 at 17:07









        Jeff Schaller

        31.1k846105




        31.1k846105






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438772%2fpipe-from-a-while-loop-to-a-command-but-execute-another-command-if-pipe-command%23new-answer', 'question_page');

            );

            Post as a guest













































































            QxZ1abImKSd2 A91ukVT3C Gf7jmI01 TefhWA4EeDK0jb,H2
            l geM Z3W X LOjDy Rq1EZ,kYO9SmTUAtK,1LSjHbEo,VMmmAE Opi6xhk53wBGCBO5smT,71Vlr,kg2bOghFg,Ffoevq,W qKZ y1yCvi2NB0

            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            How many registers does an x86_64 CPU actually have?

            Displaying single band from multi-band raster using QGIS