how to get the pid

how to get the pid

Post by Thomas Zande » Fri, 02 Mar 2001 19:24:37



Hello,

I would like to get the pid "automatically" by starting a process.

To be more precise: I would like to start a process by means of a script
that (when the process has started) manipulates this process. For this
purpose I need the pid of the just started process.

Is there an easy way without ps and grep? When I start the process
manually in a csh, there appears the pid as the second string. Can I
make use of this output in a script?

Thanks for hints, Thomas

 
 
 

how to get the pid

Post by John Pritchard-William » Fri, 02 Mar 2001 21:34:53


You can get hold of the current PID by examing the variable $$ - I'm not
sure if this helps you or not.

John

eg)
#/bin/sh
echo "Welcome to my script etc....."
echo "The current PID is "$$


Quote:> Hello,

> I would like to get the pid "automatically" by starting a process.

> To be more precise: I would like to start a process by means of a script
> that (when the process has started) manipulates this process. For this
> purpose I need the pid of the just started process.

> Is there an easy way without ps and grep? When I start the process
> manually in a csh, there appears the pid as the second string. Can I
> make use of this output in a script?

> Thanks for hints, Thomas


 
 
 

how to get the pid

Post by Anthony Borl » Fri, 02 Mar 2001 21:49:27



Quote:> Hello,

> I would like to get the pid "automatically" by starting a process.

> To be more precise: I would like to start a process by means of a script
> that (when the process has started) manipulates this process. For this
> purpose I need the pid of the just started process.

> Is there an easy way without ps and grep? When I start the process
> manually in a csh, there appears the pid as the second string. Can I
> make use of this output in a script?

You may obtain the 'process id' (pid) a few ways. Here are a couple:

* Directly from the script from which a process was invoked via:

  - $$ The pid of the current shell invocation
  - $!  The pid of the last launched background command

  So, for example:

  ./myBackgroundJob &
   mBJpid=$!;

   echo "My background job pid is: $mBJpid";

* Via the 'ps' (process status) command:

  ./myBackgroundJob &
   mBJpid==`ps | grep 'myBackgroundJob' | grep -v 'grep' | cut -d' ' -f1`

   echo "My background job pid is: $mBJpid";

The above applies to Bourne (and derived) shells. I don't know what
facilities 'csh' offers in this regard.

I hope this helps.

 
 
 

how to get the pid

Post by Edouard Merci » Fri, 02 Mar 2001 22:04:51




Quote:>Hello,

>I would like to get the pid "automatically" by starting a process.

>To be more precise: I would like to start a process by means of a script
>that (when the process has started) manipulates this process. For this
>purpose I need the pid of the just started process.

>Is there an easy way without ps and grep? When I start the process
>manually in a csh, there appears the pid as the second string. Can I
>make use of this output in a script?

>Thanks for hints, Thomas

hi Thomas;

I'va got something for you. I don't know if you're like it but il solves
the problem.
The only thing is that it uses some PERL script. See bellow.

If you replace Thomas_process correctly, it will work fine: you'll get
Thomas_process id in variable ${process_id} and in the else branch.

Is it enough ?

Edouard

#!/usr/local/bin/perl -w

$process_id = fork();
if (${process_id} == 0)
{
#this is the child
    print("Child id $$\n");
    exec(". Thomas_process");

Quote:}

else
{
#this is the parent
    print("Parent id $$\n");
    print("Parent Child id ${process_id}\n");

Quote:}

---
Posted via freenews.netfront.net

 
 
 

how to get the pid

Post by Thomas Zande » Fri, 02 Mar 2001 23:37:05


$$ and $! work but not in the csh

ps does not work as several jobs with the same name exist.

Does someone know the translation to the csh?

#!/bin/bsh
xcalc&
echo $!
kill $!

This example echos the pid of xcalc and than kills xcalc (just an example)

 
 
 

how to get the pid

Post by Sven Maschec » Sat, 03 Mar 2001 00:48:01


 > $$ and $! work but not in the csh

I bet that's wrong even for your version?  See the manpage about $$ and
$child!  Obiously you missed important things from the manpage, see a
posting named "manpages" in this group for how to ease reading them.

 > X-Mailer: [...] IRIX64 6.5 IP30

Fine in /bin/csh on both 'IRIX 6.5' and 'IRIX64 6.5' (6.5.10f) here.

Sven

 
 
 

how to get the pid

Post by Bill Marcu » Sat, 03 Mar 2001 04:38:20



>$$ and $! work but not in the csh

>ps does not work as several jobs with the same name exist.

>Does someone know the translation to the csh?

Some things just can't be translated.
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/index.html
 
 
 

how to get the pid

Post by Thomas Zande » Sat, 03 Mar 2001 22:36:28




>  > $$ and $! work but not in the csh

> I bet that's wrong even for your version?  See the manpage about $$ and
> $child!  Obiously you missed important things from the manpage, see a
> posting named "manpages" in this group for how to ease reading them.

that not very helpfull, Sven.  What I wanted to say is, that this works:

#!/bin/bsh
xcalc&
echo $!
kill $!

but this doesn't:

#!/bin/csh
xcalc&
echo $!
kill $!

Probably the syntax is incorrect. Do you know the correct one?

Dankeschoen,

Thomas

 
 
 

how to get the pid

Post by Sven Maschec » Sat, 03 Mar 2001 23:42:09


 >>> [IRIX]
 >>> $$ and $! work but not in the csh

 >> [...] See the manpage about $$ and $child!

 > #!/bin/csh   [... but with sh syntax]
 > Probably the syntax is incorrect. Do you know the correct one?

I do, after reading csh(1), although i don't use csh.
Why do you insist in not reading the manpage and using the variables
which i even had mentioned above, instead?

Common note:  $child is not portable csh (nor tcsh) syntax, but available
on the mentioned IRIX versions ("IRIX 6.5:1286818620 built 10/11/00
at zebub:/xlv51/6.5.10f/root").

We better ?switch? to email now.

[f'up2p]
Sven, no *csh user

 
 
 

1. How to make a RSH and getting is PID.

Is there a way I can do a RSH, then get his pid ?
I`m trying to program an exe that will start a RSH and then start a
process on the remonte computer.  There's a way to do that I'm sure.
What are the systems call I must do ?  Is it possible I can do it in one
command like

rsh  ( parameters ) | process_to_start

Thanks for your answer.

2. 3c905C on Solaris 8 : anyone using this card ?

3. Getting the PID

4. pdf tool for the researcher

5. getting process pid

6. Curses problem on PC monitors

7. problem getting the pid

8. Magic TView no sound with bttv

9. Getting the PID of a subshell

10. getting a PID

11. Getting UTMP.pid file

12. Getting the pid of a process reading from a fifo

13. Getting a PID using cat