Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to see the processId of the process called by system command from perl

by advait (Beadle)
on Apr 16, 2008 at 20:04 UTC ( #680907=perlquestion: print w/ replies, xml ) Need Help??
advait has asked for the wisdom of the Perl Monks concerning the following question:

Hi All
I am calling a R script from perl by system command. I want to see the processID of the R script. Can you please suggest how can I do it.

Thank you in advance
vidhu

Comment on How to see the processId of the process called by system command from perl
Re: How to see the processId of the process called by system command from perl
by pc88mxer (Vicar) on Apr 16, 2008 at 20:13 UTC
    You'll need to fork the R script yourself like this:
    my $pid; defined($pid = fork()) or die "unable to fork: $!\n"; if ($pid == 0) { # child exec(...); # exec R script here die "unable to exec: $!\n"; } # parent continues here
    Note that depending on how you call exec, $pid will either be the pid of the R command or the pid of a shell that perl creates to invoke the R command. Examples:
    exec("R", "and", "some", "args"); # $pid will be the R command exec("R and some args > out"); # $pid will be that of a shell
Re: How to see the processId of the process called by system command from perl
by elmex (Friar) on Apr 16, 2008 at 20:14 UTC
    How are you calling the R script? With the system function? Are you forking before you call it? Is the script going to the background? Best way would probably be to fork and call the R script in that new process via system:
    my $pid = fork; defined $pid or die "fork failed: $!"; if ($pid) { # in parent: waitpid $pid; # wait for the child, which's pid is in $pid } else { # in child: system ("R ..."); exit; }
Re: How to see the processId of the process called by system command from perl
by apl (Monsignor) on Apr 16, 2008 at 20:17 UTC
Re: How to see the processId of the process called by system command from perl
by BrowserUk (Pope) on Apr 16, 2008 at 22:06 UTC
Re: How to see the processId of the process called by system command from perl
by walto (Pilgrim) on Apr 17, 2008 at 04:03 UTC
    On Linux you can use:
    my $pid = `pidof R`;
      with pidof how to get the output of othe command which gets executed. For example if the command to be executed is "date" I want to get the output of the command to a perl variable and also the pid number.

        Have you read the pidof manpage? It tells you what pidof does. It only finds the pid of a running process. It does not launch a process.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://680907]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-12-28 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls