From: Dave on 22 Jul 2010 14:17 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 22 Jul 2010 17:57 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 22 Jul 2010 18:04 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 22 Jul 2010 21:25 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 23 Jul 2010 09:39 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%% *}
|
Pages: 1 Prev: CPA AFFILIATE NETWORKS AWESOME EARNINGS FROM YOUR HOME Next: GNU Parallel 20100722 released |