<div dir="ltr">I think you are correct on the RHS of the pipe.  If you are not adverse to bash, this appears to work as expected:<div><br></div><div><br><div>#!/bin/bash<br><br>echo "i am doing something useful"<br>sleep 3 &<br>wait<br>exec bash <(curl -s $URL/test.sh)<br></div></div><div><br></div><div><br></div><div>if bash is a no-go, (simply needed for the <() operation), you may just need to download a temporary file and e.g. 'exec sh /tmp/script.sh'</div><div><br></div><div>I think the crux of it is the exec replaces the current process with whatever you provide it.  But on the RHS of the pipe, that current process is a subshell</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jun 30, 2020 at 11:06 AM Adam Thompson <<a href="mailto:athompso@athompso.net">athompso@athompso.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Even that doesn't quite work:<br>
<br>
root@bgpmirror:~# pstree -g 3 -w -s screen<br>
─┬= 00001 root /sbin/init<br>
  └─┬= 04264 root sshd: /usr/sbin/sshd [listener] 0 of 10-100 startups <br>
(sshd)<br>
    └─┬= 20865 root sshd: root@ttyp0,ttyp2,ttyp1 (sshd)<br>
      └─┬= 73703 root -ksh (ksh)<br>
        └─┬= 59765 root screen -U -q -i -fa<br>
          └─┬= 01077 root SCREEN -U -q -i -fa (screen)<br>
            └─┬= 03339 root /bin/ksh<br>
              └─┬─ 93663 root sh -c curl -s <br>
<a href="https://lg.merlin.ca/ping/go.sh" rel="noreferrer" target="_blank">https://lg.merlin.ca/ping/go.sh</a> | sh<br>
                └─┬─ 87697 root sh -c curl -s <br>
<a href="https://lg.merlin.ca/ping/go.sh" rel="noreferrer" target="_blank">https://lg.merlin.ca/ping/go.sh</a> | sh<br>
                  └─┬─ 25186 root sh -c curl -s <br>
<a href="https://lg.merlin.ca/ping/go.sh" rel="noreferrer" target="_blank">https://lg.merlin.ca/ping/go.sh</a> | sh<br>
                    └─┬─ 85474 root sh<br>
                      └─── 11715 root sleep 60<br>
<br>
Argh.<br>
<br>
FYI, I also tried "exec ( curl ... | sh)" which did not work, it <br>
produced an error instead.<br>
<br>
A loop isn't suitable, because I want to be able to pick up any changes <br>
to the script on the fly - that's why I'm doing it this way in the first <br>
place.<br>
<br>
-Adam<br>
<br>
<br>
On 2020-06-30 10:36, Gilbert E. Detillieux wrote:<br>
> Adam,<br>
> <br>
> I think you're correct in your assumption about the problem being with<br>
> the pipe.  IIRC, the shell spawns a child to manage each pipeline, so<br>
> that it can properly handle redirects, signals, etc., as it sets up<br>
> the pipeline. In the case of the original Bourne shell, the child<br>
> shell process then does an exec on the last binary in the pipeline, so<br>
> its exit status is used as the exit status of the entire pipeline.<br>
> <br>
> So, the explicit exec you are doing has no real effect, as it's<br>
> happening in a child process, and not the parent shell.<br>
> <br>
> Likely also, YMMV depending on which specific shell you use.<br>
> <br>
> Not sure if this would work better?...<br>
> <br>
> exec sh -c "curl -s $URL/go.sh | sh"<br>
> <br>
> You still have parent & child shell processes, but I'm not sure if<br>
> they'll exit differently.  Maybe you'd need to background the pipeline<br>
> to allow the parent shell to exit, and the background pipeline to take<br>
> over?<br>
> <br>
> Or just do a loop, as Rob suggested.  :)<br>
> <br>
> Gilbert<br>
> <br>
> On 2020-06-30 10:16 a.m., Adam Thompson wrote:<br>
>> I've written a shell script that redownloads itself and re-executes <br>
>> itself, but I'm not quite seeing the behaviour I'd expect out of "exec <br>
>> sh".  Instead of one process, I get an infinite progression of shells <br>
>> that finally blows up when I hit ulimits.<br>
>> <br>
>> The script:<br>
>> <br>
>> ===BOF===<br>
>> curl -s $URL/targets \<br>
>> | while read TGTNAME TGTIP ; do<br>
>>      (<br>
>>          echo -n "."<br>
>> <br>
>>          mtr \<br>
>>              --interval 0.1 \<br>
>>              --gracetime 1 \<br>
>>              --timeout 1 \<br>
>>              --report-wide \<br>
>>              --report-cycles 600 \<br>
>>              --show-ips \<br>
>>              --aslookup \<br>
>>              --no-dns \<br>
>>              ${TGTIP} \<br>
>>          | awk -f parse.awk -v TGT=${TGTNAME}  \<br>
>>          | psql -q -b -h $PGHOST -U $PGUSER $PGDB<br>
>>      ) &<br>
>>     wait &<br>
>> done<br>
>> <br>
>> # start all over again<br>
>> sleep 60 &<br>
>> wait<br>
>> curl -s $URL/go.sh | exec sh<br>
>> ===EOF===<br>
>> <br>
>> The key items are the backgrounding of mtr/awk/psql, which appears to <br>
>> work properly - there's 12 lines in "targets" and I only see 12 <br>
>> mtr/awk/psql process groups at a time - the sleeping + waiting (which <br>
>> theoretically reaps all children and grandchildren, I think) and the <br>
>> final "exec".<br>
>> <br>
>> Except instead of exec'ing, I run an infinite sequence of shells, each <br>
>> one a child of the previous.  E.g. from pstree(1):<br>
>> <br>
>> # pstree -lp 1628<br>
>> screen(1628)─┬─bash(1629)───sh(12707)───sh(12854)───sh(12986)───sh(13154)───sh(13309)───sh(13444)───sh(13579)───sh(13709)───sh(13842)───sh(13979)───sh(14101)───sh(14242)───sh(14396)───sh(14530)───sh(14655)───sh(14795)───sh(14935)───sh(15076)───sh(15203)───sh(15353)───sh(15490)───sh(15624)───sh(15753)───sh(15891)───sh(16023)───sh(16154)───sh(16282)───sh(16418)───sh(16551)───sh(16696)───sh(16842)───sh(16975)───sh(17112)───sh(17241)───sh(17388)───sh(17522)───sh(17664)───sh(17824)───sh(17956)───sh(18096)───sh(18233)───sleep(18279) <br>
>> I think the problem has something to do with the fact the final exec <br>
>> is on the RHS of a pipe, and thus is executing inside a subshell, but <br>
>> how do I fix this?<br>
>> <br>
>> Thanks,<br>
>> -Adam<br>
_______________________________________________<br>
Roundtable mailing list<br>
<a href="mailto:Roundtable@muug.ca" target="_blank">Roundtable@muug.ca</a><br>
<a href="https://muug.ca/mailman/listinfo/roundtable" rel="noreferrer" target="_blank">https://muug.ca/mailman/listinfo/roundtable</a><br>
</blockquote></div>