From: Dave on
Gang,

I executing a piped command in the background from a bash
script. I would like to obtain that PID for later termination if
desired by the user. So I tried using the $! variable in bash, but it
gives the PID of the piped command, not the parent command. For
example...

tar ... | sed ... > test.log &

Using $! right after that call in the script will return the PID of
the sed program, not the tar program. And upon killing the returned
PID, I get a "Broken pipe" error (of course). Does anyone know how to
resolve this problem?

Thanks,
Dave
From: Janis Papanagnou on
On 22/07/10 20:17, Dave wrote:
> Gang,
>
> I executing a piped command in the background from a bash
> script. I would like to obtain that PID for later termination if
> desired by the user. So I tried using the $! variable in bash, but it
> gives the PID of the piped command, not the parent command. For
> example...
>
> tar ... | sed ... > test.log &
>
> Using $! right after that call in the script will return the PID of
> the sed program, not the tar program. And upon killing the returned
> PID, I get a "Broken pipe" error (of course). Does anyone know how to
> resolve this problem?

How about enclosing the pipe commands in a subshell and killing the
whole pipeline...?

( tar ... | sed ... > test.log ) &


Janis

>
> Thanks,
> Dave

From: Maxwell Lol on
Dave <hendedav(a)gmail.com> writes:

> Gang,
>
> I executing a piped command in the background from a bash
> script. I would like to obtain that PID for later termination if
> desired by the user. So I tried using the $! variable in bash, but it
> gives the PID of the piped command, not the parent command. For
> example...
>
> tar ... | sed ... > test.log &
>
> Using $! right after that call in the script will return the PID of
> the sed program, not the tar program. And upon killing the returned
> PID, I get a "Broken pipe" error (of course). Does anyone know how to
> resolve this problem?


Assuming there is only one parent process at a time...

The parent may create a file, like "myprocess.pid" using
echo $$ >myprocess.pid

You can retreive this later.

From: John DuBois on
In article <44bdd15a-5c97-4dbc-b6c5-55ed07c859aa(a)l14g2000yql.googlegroups.com>,
Dave <hendedav(a)gmail.com> wrote:
>Gang,
>
> I executing a piped command in the background from a bash
>script. I would like to obtain that PID for later termination if
>desired by the user. So I tried using the $! variable in bash, but it
>gives the PID of the piped command, not the parent command. For
>example...
>
>tar ... | sed ... > test.log &
>
>Using $! right after that call in the script will return the PID of
>the sed program, not the tar program. And upon killing the returned
>PID, I get a "Broken pipe" error (of course). Does anyone know how to
>resolve this problem?

In bash:

coproc tar ...
# bash seems to close high fds for processes that will be executed
# asynchronously, thus the strange redirection
{ sed ... > test.log & } <&${COPROC[0]}
....
kill $COPROC_PID

In ksh:

tar ... |&
p=$!
sed ... > test.log <&p &
....
kill $p

John
--
John DuBois spcecdt(a)armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
From: Dave on
On Jul 22, 5:57 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> On 22/07/10 20:17, Dave wrote:
>
> > Gang,
>
> >      I executing a piped command in the background from a bash
> > script.  I would like to obtain that PID for later termination if
> > desired by the user.  So I tried using the $! variable in bash, but it
> > gives the PID of the piped command, not the parent command.  For
> > example...
>
> > tar ... | sed ... > test.log &
>
> > Using $! right after that call in the script will return the PID of
> > the sed program, not the tar program.  And upon killing the returned
> > PID, I get a "Broken pipe" error (of course).  Does anyone know how to
> > resolve this problem?
>
> How about enclosing the pipe commands in a subshell and killing the
> whole pipeline...?
>
>   ( tar ... | sed ... > test.log ) &
>
> Janis
>
>
>
> > Thanks,
> > Dave


Thanks for all the replies. I didn't want to store in a file (as in
Maxwell's reply). I also didn't try the above, but it seems
promising. I take it that syntax will create a single PID?

Anyhow, I came up with another solution using ps and grep to filter
the tar command associated with the parent process (via passing $$ to
ps). Let me know if one solution is better than the other.

Dave

# displays all the child processes from parent process, filtering the
tar command
CHILDPID=`ps -m --ppid $$|grep tar$`
# remove preceeding spaces
CHILDPID=${CHILDPID#* }
# remove everything after the PID
CHILDPID=${CHILDPID%% *}