CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Getting PID of executed program

 



Jean
User


Feb 20, 2001, 8:59 AM

Post #1 of 4 (150 views)
Getting PID of executed program Can't Post

Hi All,

I'd like to obtain the pid of the process that is being executed from the script with system or any other function, e.g.

$pid = system ("someprog");
print "The last PID is $pid";


10x in advance,
Jean



japhy
Enthusiast

Feb 20, 2001, 9:40 AM

Post #2 of 4 (149 views)
Re: Getting PID of executed program [In reply to] Can't Post

You can get that by forking your program, or by using open():


Code
if ($pid = fork) { 
# this is the parent
}
elsif (defined $pid) {
# this is the child
exec 'some_prog', 'foo', 'bar';
}
else {
die "problem forking: $!";
}

# or

$pid = open(PROG, "some_prog 'foo' 'bar' |")
or die "problem forking: $!";

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Jean
User


Feb 20, 2001, 10:42 AM

Post #3 of 4 (148 views)
Re: Getting PID of executed program [In reply to] Can't Post

Maybe I've misunderstood the whole concept here,
but what I want is to get the pid of an *external* process.
Like I'm running some daemon and would like to save its pid for future refence.

The situation is like this:
I'm executing the same daemon several times with different parameters and would like to be able to kill
some specific instance of the daemon if I have to.
The killing is done by reading a text file that was is updated in the mentioned above part, i.e.
1. You run the program
2. You save the program's pid in the text file
3. Excute steps 1-2 several times
4. On demand you open the text file and kill the specified
program instance by using one of the saved pids


Jean
QA Engineer @ http://www.extent.com
mage@lycosmail.com


japhy
Enthusiast

Feb 20, 2001, 11:40 AM

Post #4 of 4 (145 views)
Re: Getting PID of executed program [In reply to] Can't Post

If you write a program to SPAWN a process, then you can save the PID of that process.


Code
#!/usr/bin/perl -w 

use strict;
my $program = "/path/to/prog";

if (my $pid = fork) {
print "Executing: $program @ARGV\n";
print "PID = $pid\n";
exit;
}
else {
exec $program, @ARGV;
}

You can have it save the PID (and the args, stored in @ARGV) to a file, if you'd like.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives