how to get process id in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get process id in perl
# 1  
Old 07-31-2009
how to get process id in perl

Hey guys,

how can i get process id of some particular command in perl?

example:

$cmd = "date; sleep 10; date&";
system($cmd);

How can i get process id of system command?
# 2  
Old 07-31-2009
system() does something like this in bad perl code:
Code:
$pid=fork();
if ($pid==0)
{
       exec(''/usr/bin/sh my command goes here');
}
$retval=wait;
print  "$pid\n";

$pid in this case is the pid of the child process, not the pid of the exec'ed process - the process actaully running the command.

exec returns nothing when successful.

This is why it is hard to get the process pid in a system call. I would simply change the command:
Code:
$cmd = "echo $$ > ./pidfile;date; sleep 10; date&";

./pidfile contains the pid of the child process.
# 3  
Old 07-31-2009
thanks jim,

second code work for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parallel process in Perl

HI All, I have scenerio where I need to call sub modules through for loop for (i=0; i<8000 ;i++) { .. BLOCKA } BLOCKA { .. .. subroutine 1; subroutine 2; } I want this to be run in parallel process1 BLOCKA { (6 Replies)
Discussion started by: gvk25
6 Replies

2. Shell Programming and Scripting

How to call a background process in perl?

Hi, I want to put the following code as a parallel or background process The program is as below: $n=10; #Count of files to be created. for($j=0;$j<=$n;$j++) { open(FH,">files_$j.txt") || warn "cannot create a file\n"; { print FH "count of file: $j\n"; #Sample data to be written. just... (5 Replies)
Discussion started by: vanitham
5 Replies

3. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

4. Shell Programming and Scripting

Perl : Process a file in perl

I have a file with following data : Qspace: COLOR: Queue: doColor Qspace: COLOR: Queue order: fifo Qspace: COLOR: Out of order: none Qspace: COLOR: Retries: 5 Qspace: COLOR: Queue: setColor Qspace: COLOR: Queue order: fifo Qspace: COLOR: Out of order: none Qspace: COLOR: Retries: 5... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

5. Shell Programming and Scripting

Perl : Kill process within 5 min

From a perl script , How can I monitor a PS which I activated and kill it within 5 minutes in case it didn't complete its tasks.:confused: (2 Replies)
Discussion started by: Alalush
2 Replies

6. Shell Programming and Scripting

Perl Background Process - Finshed Yet?

I have a perl process I want to run in background in a cgi, but do not want to continue until process finished code looks like this. sub calc(){ do calculations return value } In another Perl program I call above function such as &calc(); I want to continue after process... (4 Replies)
Discussion started by: photon
4 Replies

7. Shell Programming and Scripting

to kill a process in perl

Hi friends, I have a perl script which needs to kill all java processes running on both windows and unix. currently I'm getting the OS and killing the process by using system. my code is: if ($os eq MSWin32) system("taskkill java"); else system("kill -9 java") Is there any way... (2 Replies)
Discussion started by: gurukottur
2 Replies

8. Shell Programming and Scripting

Perl: Run perl script in the current process

I have a question regarding running perl in the current process. I shall demonstrate with an example. Look at this. sh-2.05b$ pwd /tmp sh-2.05b$ cat test.sh #! /bin/sh cd /etc sh-2.05b$ ./test.sh sh-2.05b$ pwd /tmp sh-2.05b$ . ./test.sh sh-2.05b$ pwd /etc sh-2.05b$ So... (10 Replies)
Discussion started by: vino
10 Replies

9. Shell Programming and Scripting

PERL: wait for process to complete

I'm using PERL on windows NT to try to run an extract of data. I have multiple zip files in multiple locations. I am extracting "*.t" from zip files and subsequently adding that file to one zip file so when the script is complete I should have one zip file with a whole bunch of ".t" files in it. ... (2 Replies)
Discussion started by: dangral
2 Replies

10. Shell Programming and Scripting

Killing a process from perl script.

How do I kill a process, say by name "drec" from a perl script. I tried with : ps -eaf | grep drec | awk '{print $2}' | xargs kill -9. The output I got is : ps -eaf | grep drec | awk '{print }' | xargs kill -9 /usr/bin/kill: ipgen: Arguments must be %job or process ids {But, $2 is not... (3 Replies)
Discussion started by: sharuvman
3 Replies
Login or Register to Ask a Question