<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div>Oh! That's a neat idea, will try it out but I still want to understand what I'm doing wrong with my trap. <br></div><div><br></div><div>I think my wait on cat is causing a problem. Maybe I should be putting my whole while loop into a subshell?</div><div><br></div><div>On Sun, Jul 28, 2024, at 12:56 PM, Chris Frey wrote:<br></div><blockquote type="cite" id="qt" style=""><div>I'd recommend using some other mechanism of "end of stream" rather<br></div><div>than killing your script, since, as you found out, it halts all processing.<br></div><div><br></div><div>Perhaps instead of kill, rename the file:<br></div><div><br></div><div>echo "More data to hash" > $FIFO_FILE<br></div><div># rename it so while loop exits<br></div><div>mv "$FIFIO_FILE" "$FIFO_FILE-finished"<br></div><div># send empty data to trigger a fifo pipe close on the waiting cat<br></div><div>cat /dev/null > "$FIFO_FILE-finished"<br></div><div><br></div><div>- Chris<br></div><div><br></div><div><br></div><div>On Sun, Jul 28, 2024 at 12:18:43PM -0400, John Steel via kwlug-disc wrote:<br></div><div>> I’m having trouble figuring out how to manage a subprocess… I’m probably overcomplicating this… <br></div><div>> <br></div><div>> I’ve made a script called checksum.sh. I want it to keep moving data from a fifo to sha256sum until it’s terminated. But it seems when I kill it the cat and sha256sum are gone before my finalize method is called? If I can kill cat and let that sub shell exit gracefully I think I should see the sha256sum come out on stderr. <br></div><div>> <br></div><div>> #!/bin/bash<br></div><div>> # This is checksum.sh<br></div><div>> <br></div><div>> FIFO_FILE="${FIFO_FILE:-sha256sum_fifo}"<br></div><div>> CAT_PID_FILE="$(mktemp -p /dev/shm $$_cat_pid.XXXXXX)"<br></div><div>> export CAT_PID_FILE FIFO_FILE<br></div><div>> mkfifo "$FIFO_FILE"<br></div><div>> <br></div><div>> finalize() {<br></div><div>> pgrep -fl "cat fifo $FIFO_FILE" > /dev/stderr<br></div><div>> pgrep -fl sha256sum > /dev/stderr<br></div><div>> <br></div><div>> cat_pid="$(cat "$CAT_PID_FILE")"<br></div><div>> rm "$CAT_PID_FILE"<br></div><div>> kill -s SIGTERM "$cat_pid" && wait "$cat_pid"<br></div><div>> exit 0<br></div><div>> }<br></div><div>> <br></div><div>> trap finalize SIGTERM<br></div><div>> trap finalize SIGHUP <br></div><div>> trap finalize SIGINT <br></div><div>> trap finalize SIGQUIT <br></div><div>> <br></div><div>> # Start the cat process and capture its PID<br></div><div>> while [ -f "$CAT_PID_FILE" ]; do <br></div><div>> cat "$FIFO_FILE" & cat_pid=$!<br></div><div>> echo "$cat_pid" > "$CAT_PID_FILE"<br></div><div>> wait "$cat_pid"<br></div><div>> sleep 0.01<br></div><div>> done | sha256sum > /dev/stderr<br></div><div>> <br></div><div>> Here’s how I’m running it: <br></div><div>> <br></div><div>> docker run -it -v $PWD:/app alpine sh -xc '<br></div><div>> apk add bash procps<br></div><div>> export FIFO_FILE="$(mktemp -u -p /dev/shm fifo_$$.XXXXXX)"<br></div><div>> bash -x /app/checksum.sh &<br></div><div>> sleep 0.2<br></div><div>> jobs<br></div><div>> ps -ef --forest<br></div><div>> echo "First data to hash" > $FIFO_FILE<br></div><div>> sleep 0.2<br></div><div>> ps -ef --forest<br></div><div>> echo "More data to hash" > $FIFO_FILE<br></div><div>> kill %1<br></div><div>> sleep 1<br></div><div>> ‘<br></div><div>> <br></div><div>> Am I missing something obvious? If it’s possible without the loop that’d be great too but I found that if I connect sha256sum directly to the fifo it exits after the first write to the fifo.<br></div><div>> <br></div><div>> Should I learn how to use socat for this? From a few of the things I’ve read it sounds like it could make this simpler? <br></div><div><br></div><div><br></div><div><br></div><div>> _______________________________________________<br></div><div>> kwlug-disc mailing list<br></div><div>> To unsubscribe, send an email to <a href="mailto:kwlug-disc-leave@kwlug.org">kwlug-disc-leave@kwlug.org</a><br></div><div>> with the subject "unsubscribe", or email<br></div><div>> <a href="mailto:kwlug-disc-owner@kwlug.org">kwlug-disc-owner@kwlug.org</a> to contact a human being.<br></div><div><br></div><div><br></div><div>_______________________________________________<br></div><div>kwlug-disc mailing list<br></div><div>To unsubscribe, send an email to <a href="mailto:kwlug-disc-leave@kwlug.org">kwlug-disc-leave@kwlug.org</a><br></div><div>with the subject "unsubscribe", or email<br></div><div><a href="mailto:kwlug-disc-owner@kwlug.org">kwlug-disc-owner@kwlug.org</a> to contact a human being.<br></div><div><br></div></blockquote></body></html>