help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: left side of pipe


From: Greg Wooledge
Subject: Re: left side of pipe
Date: Sun, 23 Jan 2022 16:17:02 -0500

On Sun, Jan 23, 2022 at 08:33:27PM +0100, Pascal wrote:
> ewfacquier -t /tmp/test /dev/sda1 |
> yad --progress --progress-text "copying..." --pulsate --auto-close
> --auto-kill --button=Cancel:252

I'm guessing "yad" is an X11 program, not a terminal program.

> it works fine as long as I don't stop yad.
> if I stop yad, the ewfacquire process continues to run for a few more
> (long) seconds even though the script is finished.

That's normal and expected behavior.  When a pipe is destroyed, the
process that's writing to the pipe will receive a SIGPIPE the next
time it attempts to write to the pipe.  That's the point where it dies.

> how to stop ewfacquire when canceling yad (while keeping the pipe exit code
> in the opposite case for the if test) ?

You could simply let it go until it dies to SIGPIPE on its next attempted
write.  This is how most things work.

If you feel you MUST kill it as soon as possible, then you'll have to
rewrite basically every single thing you're doing to achieve that goal.
Instead of using an anonymous pipe, use a named pipe, and run the reader
and writer processes as background jobs with the PIDs remembered by the
script.  Set up a "wait -n" on the two PIDs, and when the wait returns
(meaning that one of them has died), send a SIGTERM to both PIDs, so
that the other one will also be killed.  Then clean up the named pipe.

#!/bin/bash
mkdir -p ~/.cache
pipe=$HOME/.cache/ewf-yad-fifo
trap 'rm -f "$pipe"' EXIT
mkfifo "$pipe"
ewfacquier -t /tmp/test /dev/sda1 > "$pipe" & writer=$!
yad --progress --progress-text "copying..." --pulsate --auto-close \
    --auto-kill --button=Cancel:252 < "$pipe" & reader=$!
wait -n "$reader" "$writer"
kill "$reader" "$writer"

But... wait, you also said you wanted to retrieve an exit status.  That
makes it even harder.  At this point, you're going to have to make some
additional assumptions.

Let's make this assumption: "the reader (yad) will always exit first".

>From there, we can just wait on the reader, and retrieve its exit status
that way.  Then we only have to kill the writer.

#!/bin/bash
mkdir -p ~/.cache
pipe=$HOME/.cache/ewf-yad-fifo
trap 'rm -f "$pipe"' EXIT
mkfifo "$pipe"
ewfacquier -t /tmp/test /dev/sda1 > "$pipe" & writer=$!
yad --progress --progress-text "copying..." --pulsate --auto-close \
    --auto-kill --button=Cancel:252 < "$pipe" & reader=$!
wait "$reader"
status=$?
kill "$writer"
exit "$status"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]