|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I would like to run in subshell some script and redirect it into
another script. Something like this: my_first_script 2>&1 | my_second_script & But I also want to save the PID of process of "my_first_script" for future use. I know that in bash, I can use $! variable to save the PID. But, of course, after previous command the $! contains PID of "my_second_script", not "my_first_script" which I want. Any suggestions how to solve it? Thanks for all hints |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Mar 13, 3:18 pm, Teni <t...@volny.cz> wrote:
> I would like to run in subshell some script and redirect it into > another script. Something like this: > > my_first_script 2>&1 | my_second_script & > > But I also want to save the PID of process of "my_first_script" for > future use. > I know that in bash, I can use $! variable to save the PID. But, of > course, after previous command the $! contains PID of > "my_second_script", not "my_first_script" which I want. > > Any suggestions how to solve it? > Thanks for all hints I have a cheap cheat, not really an ingenious solution: Since my_first_script is a shell script, it can always retrieve $$ from it and save it in a file, and can be retrieved later |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 13 Bøe, 13:43, Jeenu <jee...@gmail.com> wrote:
> On Mar 13, 3:18 pm, Teni <t...@volny.cz> wrote: > > > I would like to run in subshell some script and redirect it into > > another script. Something like this: > > > my_first_script 2>&1 | my_second_script & > > > But I also want to save the PID of process of "my_first_script" for > > future use. > > I know that in bash, I can use $! variable to save the PID. But, of > > course, after previous command the $! contains PID of > > "my_second_script", not "my_first_script" which I want. > > > Any suggestions how to solve it? > > Thanks for all hints > > I have a cheap cheat, not really an ingenious solution: > Since my_first_script is a shell script, it can always retrieve $$ > from it and save it in a file, and can be retrieved later Well, generally, "my_first_script" can also by a program, I need some general-purpose solution... But thank You anyway, good idea although it is not usable for my purpose. Any other ideas? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Teni wrote:
> I would like to run in subshell some script and redirect it into > another script. Something like this: > > my_first_script 2>&1 | my_second_script & > > But I also want to save the PID of process of "my_first_script" for > future use. > I know that in bash, I can use $! variable to save the PID. But, of > course, after previous command the $! contains PID of > "my_second_script", not "my_first_script" which I want. > > Any suggestions how to solve it? > Thanks for all hints A (not so) dirty trick might be this: $ ps | grep my_first_script and go from there to extract the PID with the usual tools. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 13 Bøe, 14:26, pk <p...@pk.pk> wrote:
> Teni wrote: > > I would like to run in subshell some script and redirect it into > > another script. Something like this: > > > my_first_script 2>&1 | my_second_script & > > > But I also want to save the PID of process of "my_first_script" for > > future use. > > I know that in bash, I can use $! variable to save the PID. But, of > > course, after previous command the $! contains PID of > > "my_second_script", not "my_first_script" which I want. > > > Any suggestions how to solve it? > > Thanks for all hints > > A (not so) dirty trick might be this: > > $ ps | grep my_first_script > > and go from there to extract the PID with the usual tools. Well, fine trick but there may be quite a lot of "my_first_script" running in a system so it can be a bit complicated to recognize which one is the right one... I think that there should be some way how to run the "my_first_script" then read the PID ($!) then run "my_second_script" and somehow shuffle the file-descriptors to redirect stdout and stderr from "my_first_script" to "my_second_script". I am currently searching for such a solution but stil dont have it since i am not a shell-guru... |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Teni wrote:
>> A (not so) dirty trick might be this: >> >> $ ps | grep my_first_script >> >> and go from there to extract the PID with the usual tools. > > Well, > fine trick but there may be quite a lot of "my_first_script" running > in a system so it can be a bit complicated to recognize which one is > the right one... If you make certain assumptions (which of course might not be true) things could be a bit easier. For example, if you know the PID of the shell or process that started the thing, so you could do something eg $ ps --ppid $$ | grep my_first_script to restrict the results (replace $$ with the pid of the parent). Of course, this will be still useless if many "my_first_script"s have the same parent shell (or parent process, for that matter). But you might be correct in that this is probably not a good way to go. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
2008-03-13, 03:18(-07), Teni:
> I would like to run in subshell some script and redirect it into > another script. Something like this: > > my_first_script 2>&1 | my_second_script & > > But I also want to save the PID of process of "my_first_script" for > future use. > I know that in bash, I can use $! variable to save the PID. But, of > course, after previous command the $! contains PID of > "my_second_script", not "my_first_script" which I want. [...] { pid=$( exec 3>&1 >&- sh -c 'echo "$$" >&3 exec 3>&- exec my_first_script' 2>&1 | my_second_script 3>&- >&4 & ) } 4>&1 -- Stéphane |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On 13 Bøe, 16:58, pk <p...@pk.pk> wrote:
> Teni wrote: > >> A (not so) dirty trick might be this: > > >> $ ps | grep my_first_script > > >> and go from there to extract the PID with the usual tools. > > > Well, > > fine trick but there may be quite a lot of "my_first_script" running > > in a system so it can be a bit complicated to recognize which one is > > the right one... > > If you make certain assumptions (which of course might not be true) things > could be a bit easier. For example, if you know the PID of the shell or > process that started the thing, so you could do something eg > > $ ps --ppid $$ | grep my_first_script > > to restrict the results (replace $$ with the pid of the parent). Of course, > this will be still useless if many "my_first_script"s have the same parent > shell (or parent process, for that matter). > > But you might be correct in that this is probably not a good way to go. That assumption is always true in my case, so thank You, this is good enough for me and pretty simple. Teni |
|
![]() |
| Outils de la discussion | |
|
|