2 replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
source | link

I actually demonstrate exactly how this sort of thing might be done in another answer herehere. That answer was to a question about ensuring 2 logs were maintained by a background process, so I demonstrated it with 10.

I actually demonstrate exactly how this sort of thing might be done in another answer here. That answer was to a question about ensuring 2 logs were maintained by a background process, so I demonstrated it with 10.

I actually demonstrate exactly how this sort of thing might be done in another answer here. That answer was to a question about ensuring 2 logs were maintained by a background process, so I demonstrated it with 10.

1
source | link

I actually demonstrate exactly how this sort of thing might be done in another answer here. That answer was to a question about ensuring 2 logs were maintained by a background process, so I demonstrated it with 10.

Demo Script

cat <<-\DEMO >|${s=/tmp/script} 
printf 'tty is %s\nparent pid is %s\npid is pid=%s\n' \
     "$(tty)" "$PPID" "$$"
exec 1>&2 ; nums=$(seq 0 9)
rm ${files=$(printf "/tmp/file%s\n" $nums)}
for n in $nums ; do { for f in $files ; do
    echo "Line $n" >>"$f" ; done
sleep 1 ; } ; done
#END
DEMO

Run Demo

s=/tmp/script ;chmod +x $s ;info="$(($s &)2>&- &)"
echo "$info" ; pid="${info##*=}" ; echo
while ps -p $pid >/dev/null ; do sleep 3 ; done
for f in /tmp/file[0-9] ; do
    printf 'path : %s\tline count : %s\n' \
        $f $(<$f wc -l)
done

Output:

tty is not a tty
parent pid is 1
pid is 12123

path : /tmp/file0    line count : 10
path : /tmp/file1    line count : 10
path : /tmp/file2    line count : 10
path : /tmp/file3    line count : 10
path : /tmp/file4    line count : 10
path : /tmp/file5    line count : 10
path : /tmp/file6    line count : 10
path : /tmp/file7    line count : 10
path : /tmp/file8    line count : 10
path : /tmp/file9    line count : 10

The above demonstrates. It builds and runs a script named /tmp/script, chmod's it as executable, and runs it in the &background of a &backgrounded ( subshell ).

The script rms /tmp/file0-9 10 files and echoes a line every second into all 10 of them. I capture some $info from the disowned process and present it via $(command substitution). While ps still reports on the $pid I capture, I know it still runs so I sleep. When it completes, the lines in all 10 files are counted with wc.

After you invoke a process in this way you can freely close its original parent process and it will keep on trucking - it's effectively disowned. This also means you cannot use the conventional wait command, but waiting on ps's return should be more robust in any case.

Worth mentioning, I think, is that the process is actually initially called in $(command substitution) and printfs me the $info I want so I can effectively control it. But as soon as it drops its terminal output with exec 1>&2 (which is closed in the same subshell with 2>&-), the process escapes and I have to wait around for it on the other end. Kinda the best of both worlds, especially if you use it to handle input pipes, so long as you can wrap your mind around all of the redirections and process leaders.

Everything else is just for demonstration here. All you need to run this is the top script and:

info="$(($script_path &)2>&- &)"    

NOTE: This only prints to terminal exactly what I wished to demonstrate it. As noted by the $PPID, this process is disowned by the terminal and is a direct child of $PID 1.

If you wanted to run two of these concurrently and wait for them you could just hand ps both of their pids and wait.