<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">Progress! I liked your idea of having the loop finish without needing to be killed. I switched to signup and sha256sum. Removing the PID file from the trap seems to gracefully stop the loop. I still think I don’t have a good mental picture of why this works and the previous one didn't <div><br></div><div><div>$ docker run -it -v $PWD:/app alpine sh -xc '</div><div>  apk add bash procps</div><div>  export FIFO_FILE="$(mktemp -u -p /dev/shm fifo_$$.XXXXXX)"</div><div>  bash -x /app/checksum.sh &</div><div>  sleep 0.2</div><div>  jobs</div><div>  ps -ef --forest</div><div>  echo "First data to hash" > $FIFO_FILE</div><div>  sleep 0.2</div><div>  ps -ef --forest</div><div>  echo "More data to hash" > $FIFO_FILE</div><div>  sleep 1</div><div>  echo "More data to hash" > $FIFO_FILE</div><div>  sleep 1</div><div>  echo "More data to hash" > $FIFO_FILE</div><div>  kill -s SIGHUP %1</div><div>  sleep 1</div><div>'</div><div>+ apk add bash procps</div><div>fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/main/aarch64/APKINDEX.tar.gz</div><div>fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/community/aarch64/APKINDEX.tar.gz</div><div>(1/7) Installing ncurses-terminfo-base (6.4_p20231125-r0)</div><div>(2/7) Installing libncursesw (6.4_p20231125-r0)</div><div>(3/7) Installing readline (8.2.1-r2)</div><div>(4/7) Installing bash (5.2.21-r0)</div><div>Executing bash-5.2.21-r0.post-install</div><div>(5/7) Installing libintl (0.22.3-r0)</div><div>(6/7) Installing libproc2 (4.0.4-r0)</div><div>(7/7) Installing procps-ng (4.0.4-r0)</div><div>Executing busybox-1.36.1-r15.trigger</div><div>OK: 13 MiB in 22 packages</div><div>+ mktemp -u -p /dev/shm fifo_1.XXXXXX</div><div>+ export 'FIFO_FILE=/dev/shm/fifo_1.DGoldG'</div><div>+ sleep 0.2</div><div>+ bash -x /app/checksum.sh</div><div>+ FIFO_FILE=/dev/shm/fifo_1.DGoldG</div><div>++ mktemp -p /dev/shm 13_cat_pid.XXXXXX</div><div>+ CAT_PID_FILE=/dev/shm/13_cat_pid.DOBkfG</div><div>+ export CAT_PID_FILE FIFO_FILE</div><div>+ mkfifo /dev/shm/fifo_1.DGoldG</div><div>+ trap finalize SIGTERM</div><div>+ trap finalize SIGHUP</div><div>+ trap finalize SIGINT</div><div>+ trap finalize SIGQUIT</div><div>+ wait</div><div>+ + '[' tee -f /dev/stderr/dev/shm/13_cat_pid.DOBkfG </div><div>']'</div><div>+ sha256sum</div><div>+ cat_pid=20</div><div>+ echo 20</div><div>+ wait 20</div><div>+ cat /dev/shm/fifo_1.DGoldG</div><div>+ jobs</div><div>[1]+  Running                    </div><div>+ ps -ef --forest</div><div>UID        PID  PPID  C STIME TTY          TIME CMD</div><div>root         1     0  0 17:48 pts/0    00:00:00 sh -xc    apk add bash procps   export FIFO_FILE="$(mktemp -u -p /dev/shm fifo_$$.XXXXXX)"   bash </div><div>root        13     1  0 17:48 pts/0    00:00:00 bash -x /app/checksum.sh</div><div>root        18    13  0 17:48 pts/0    00:00:00  \_ tee /dev/stderr</div><div>root        17    13  0 17:48 pts/0    00:00:00  \_ bash -x /app/checksum.sh</div><div>root        20    17  0 17:48 pts/0    00:00:00  |   \_ cat /dev/shm/fifo_1.DGoldG</div><div>root        19    13  0 17:48 pts/0    00:00:00  \_ sha256sum</div><div>root        21     1  0 17:48 pts/0    00:00:00 ps -ef --forest</div><div>+ echo 'First data to hash'</div><div>+ sleep 0.2</div><div>First data to hash</div><div>+ sleep 0.01</div><div>+ '[' -f /dev/shm/13_cat_pid.DOBkfG ']'</div><div>+ cat_pid=24</div><div>+ echo 24</div><div>+ wait 24</div><div>+ cat /dev/shm/fifo_1.DGoldG</div><div>+ ps -ef --forest</div><div>UID        PID  PPID  C STIME TTY          TIME CMD</div><div>root         1     0  0 17:48 pts/0    00:00:00 sh -xc    apk add bash procps   export FIFO_FILE="$(mktemp -u -p /dev/shm fifo_$$.XXXXXX)"   bash </div><div>root        13     1  0 17:48 pts/0    00:00:00 bash -x /app/checksum.sh</div><div>root        18    13  0 17:48 pts/0    00:00:00  \_ tee /dev/stderr</div><div>root        17    13  0 17:48 pts/0    00:00:00  \_ bash -x /app/checksum.sh</div><div>root        24    17  0 17:48 pts/0    00:00:00  |   \_ cat /dev/shm/fifo_1.DGoldG</div><div>root        19    13  0 17:48 pts/0    00:00:00  \_ sha256sum</div><div>root        25     1  0 17:48 pts/0    00:00:00 ps -ef --forest</div><div>+ echo 'More data to hash'</div><div>+ sleep 1</div><div>More data to hash</div><div>+ sleep 0.01</div><div>+ '[' -f /dev/shm/13_cat_pid.DOBkfG ']'</div><div>+ cat_pid=28</div><div>+ echo 28</div><div>+ wait 28</div><div>+ cat /dev/shm/fifo_1.DGoldG</div><div>+ echo 'More data to hash'</div><div>+ sleep 1</div><div>More data to hash</div><div>+ sleep 0.01</div><div>+ '[' -f /dev/shm/13_cat_pid.DOBkfG ']'</div><div>+ cat_pid=31</div><div>+ echo 31</div><div>+ + wait 31</div><div>cat /dev/shm/fifo_1.DGoldG</div><div>+ echo 'More data to hash'</div><div>+ kill -s SIGHUP '%1'</div><div>+ sleep 1</div><div>More data to hash</div><div>++ + finalizesleep </div><div>0.01</div><div>++ pgrep -fl 'cat fifo /dev/shm/fifo_1.DGoldG'</div><div>++ pgrep -fl sha256sum</div><div>19 sha256sum</div><div>+++ cat /dev/shm/13_cat_pid.DOBkfG</div><div>++ cat_pid=31</div><div>++ rm /dev/shm/13_cat_pid.DOBkfG</div><div>++ exit 0</div><div>+ '[' -f /dev/shm/13_cat_pid.DOBkfG ']'</div><div>23d54b3983757deb3a3f775175a36a236bb82badca0e6c79423a8cc059c9059b  -</div><div><br></div><div>$ echo 'First data to hash                    </div><div>quote> More data to hash</div><div>quote> More data to hash</div><div>quote> More data to hash' | sha256sum</div><div>23d54b3983757deb3a3f775175a36a236bb82badca0e6c79423a8cc059c9059b  -</div><div><br><blockquote type="cite"><div>On Jul 28, 2024, at 1:28 PM, John Steel via kwlug-disc <kwlug-disc@kwlug.org> wrote:</div><br class="Apple-interchange-newline"><div><meta charset="UTF-8"><div style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">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 style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><br></div><div style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">I think my wait on cat is causing a problem. Maybe I should be putting my whole while loop into a subshell?</div><div style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><br></div><div style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">On Sun, Jul 28, 2024, at 12:56 PM, Chris Frey wrote:<br></div><blockquote type="cite" id="qt" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><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><span class="Apple-converted-space"> </span>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><span class="Apple-converted-space"> </span>to contact a human being.<br></div><div><br></div></blockquote><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">_______________________________________________</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">kwlug-disc mailing list</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">To unsubscribe, send an email to<span class="Apple-converted-space"> </span></span><a href="mailto:kwlug-disc-leave@kwlug.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">kwlug-disc-leave@kwlug.org</a><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">with the subject "unsubscribe", or email</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><a href="mailto:kwlug-disc-owner@kwlug.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">kwlug-disc-owner@kwlug.org</a><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;"><span class="Apple-converted-space"> </span>to contact a human being.</span></div></blockquote></div><br></div></body></html>