[RndTbl] Shell exec(2) help?

Adam Thompson athompso at athompso.net
Tue Jun 30 10:16:45 CDT 2020

I've written a shell script that redownloads itself and re-executes 
itself, but I'm not quite seeing the behaviour I'd expect out of "exec 
sh".  Instead of one process, I get an infinite progression of shells 
that finally blows up when I hit ulimits.

The script:

curl -s $URL/targets \
| while read TGTNAME TGTIP ; do
         echo -n "."

         mtr \
             --interval 0.1 \
             --gracetime 1 \
             --timeout 1 \
             --report-wide \
             --report-cycles 600 \
             --show-ips \
             --aslookup \
             --no-dns \
             ${TGTIP} \
         | awk -f parse.awk -v TGT=${TGTNAME}  \
         | psql -q -b -h $PGHOST -U $PGUSER $PGDB
     ) &
    wait &

# start all over again
sleep 60 &
curl -s $URL/go.sh | exec sh

The key items are the backgrounding of mtr/awk/psql, which appears to 
work properly - there's 12 lines in "targets" and I only see 12 
mtr/awk/psql process groups at a time - the sleeping + waiting (which 
theoretically reaps all children and grandchildren, I think) and the 
final "exec".

Except instead of exec'ing, I run an infinite sequence of shells, each 
one a child of the previous.  E.g. from pstree(1):

# pstree -lp 1628

I think the problem has something to do with the fact the final exec is 
on the RHS of a pipe, and thus is executing inside a subshell, but how 
do I fix this?


More information about the Roundtable mailing list