Pipe from a while loop to a command but execute another command if pipe command fails
Clash 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 STDOUTcompare_output.pl
: aperl
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.
bash debian
add a comment |Â
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 STDOUTcompare_output.pl
: aperl
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.
bash debian
It's not clear what this code is meant to do. Aftercompare_output.pl
dies, is the goal to runecho test
or restart thewhile
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 restartcompare_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
add a comment |Â
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 STDOUTcompare_output.pl
: aperl
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.
bash debian
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 STDOUTcompare_output.pl
: aperl
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.
bash debian
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. Aftercompare_output.pl
dies, is the goal to runecho test
or restart thewhile
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 restartcompare_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
add a comment |Â
It's not clear what this code is meant to do. Aftercompare_output.pl
dies, is the goal to runecho test
or restart thewhile
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 restartcompare_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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
up vote
2
down vote
accepted
Pipe the first loop into another loop:
while :; do ./do_stuff ; done |
while :; do ./compare_output.pl ; done
add a comment |Â
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
Pipe the first loop into another loop:
while :; do ./do_stuff ; done |
while :; do ./compare_output.pl ; done
answered Apr 19 at 17:07
Jeff Schaller
31.1k846105
31.1k846105
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It's not clear what this code is meant to do. After
compare_output.pl
dies, is the goal to runecho test
or restart thewhile
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