Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the PID from a system()

Status
Not open for further replies.

jishu

Programmer
Sep 23, 2004
3
0
0
US
Hi,

I have the following code

system ("./XYZ");

I know $$ stores the PID of the perl script. However, how can I get XYZ's pid? Is there a way to get that PID immediately? In my case XYZ can take hours to run. I may want to kill this XYZ process before it finishes. I may initiate XYZ several times before one is finished. So I can't refer them by names.

Thanks.
 
jishu - hi, you need to use fork() - like this but look in the docs for more detail

fork() splits your current process in two with you left controlling both of them

my $child_pid;
if($child_pid = fork()){
# this is the parent process, and $child_pid is the PID
# of the child process started below
sleep 3600; # wait for an hour
kill $child_pid; # if you want to...
exit 0;
} else {
exec('long/running/process');
}


Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Hi Mike,

Thanks for the reply. Is this the only way? Is there an easy way that I can get the PID directly from "System()"? I know "System()" does a fork. Is there a way to get that fork's PID?

Thanks.

Jiang
 
no... your script will wait until your system() call finishes

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
jishu,

Depending on on your OS, you can name your process at time of inception, and monitor that, and if you can't you should be able to.

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Hi,

I am actually new to fork(). I tried the following script:

my $child_pid;
if($child_pid = fork()){
system "echo \"kill -9 $child_pid\" >> ttt";
exit 0;
} else {
exec('./XYZ < a.txt');
}

In this case, I store the PID into a file called "ttt" so I can kill it later.

This script does write a PID into file "ttt", but it is a "sh" PID, not "XYZ" PID.

For example, I did a "ps -u user" at another prompt:
13438 pts/2 0:00 sh
13440 pts/2 0:06 XYZ

The above script only stores "13438" into the "ttt". However, "13440" is the PID I want to store. I tried "kill -9 13438", it didn't stop "13440".

What did I do wrong? Please advise.

Jiang

 
hi jishu,
you should try this (it's easy but i'm not that sure that's what you want):

system("./XYZ &");
system("ps -ef | grep XYZ");

bye
 
Jiang -

Two thoughts

- the thing you're running (XYZ in your example) does it start another process? How does it do that?

- also - if you do

ps -eg | grep 13438

It will pick up both process, 13440 will have 13438 as it's parent process (PPID)

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top