[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Use pid of cmd run in sub shell in parent shell?
From: |
Patrick Schleizer |
Subject: |
Re: [Help-bash] Use pid of cmd run in sub shell in parent shell? |
Date: |
Sat, 03 May 2014 17:17:00 +0000 |
Greg Wooledge:
> On Thu, May 01, 2014 at 07:46:29PM +0000, Patrick Schleizer wrote:
>> while read line; do
>> log "...: $line"
>> done < <(
>> {
>> long_running_cmd_here
>> }
>> ) &
>>
>> echo "Need pid of long_running_cmd_here here."
>>
>> =====
>>
>> I need the pid of long_running_cmd_here for job control, so I can send
>> signals to long_running_cmd_here.
>
> When you need finer control than <(...) allows, you must set it aside
> and use a named pipe.
>
> fifo=${TMPDIR:-/tmp}/myscript.$$
> trap 'rm -f "$fifo"' EXIT
> mkfifo "$fifo" || exit
> { long_running_cmd; } > "$fifo" & pid=$!
> while read line; do ...; done < "$fifo"
>
> <(...) is just a shortcut, and sometimes shortcuts don't let you do what
> you need.
I had to add a "&" behind "done < "$fifo" to make that non-blocking as well.
As well as to implement a sigterm trap inside the subshell.
Works great. Thank you, Greg for your help!
Cheers,
Patrick