PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > piping output and storing PID of process
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

piping output and storing PID of process

Réponse
 
LinkBack Outils de la discussion
Vieux 13/03/2008, 11h18   #1
Teni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut piping output and storing PID of process

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
  Réponse avec citation
Vieux 13/03/2008, 13h43   #2
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping output and storing PID of process

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
  Réponse avec citation
Vieux 13/03/2008, 14h08   #3
Teni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping output and storing PID of process

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?
  Réponse avec citation
Vieux 13/03/2008, 14h26   #4
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping output and storing PID of process

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.

  Réponse avec citation
Vieux 13/03/2008, 16h23   #5
Teni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping output and storing PID of process

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...
  Réponse avec citation
Vieux 13/03/2008, 16h58   #6
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping output and storing PID of process

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.

  Réponse avec citation
Vieux 13/03/2008, 17h04   #7
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping output and storing PID of process

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
  Réponse avec citation
Vieux 13/03/2008, 21h01   #8
Teni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping output and storing PID of process

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,33593 seconds with 16 queries