Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the PID of a process spawned with Perl's system function?

Tags:

perl

system

pid

I'm writing a Perl script (say script.pl) that calls another script newscript.pl. I want to get the PIDs of both the scripts in script.pl only. I can get the PID of script.pl by using following code

my $pid = Unix::PID->new();
my @p = $pid->get_pidof( $pid->get_command($$), 1 );

After this I call system to execute newscript.pl

system("perl newscript.pl");

I want to capture the PID generated by this newscript.pl in script.pl.

like image 970
Deepak Avatar asked Feb 22 '12 11:02

Deepak


People also ask

What is PID in Perl?

$$, $PROCESS_ID, $PID- The process number of the current script / program / executable. The process number of the current script / program / executable. Natively Perl provieds the $$ variable, but if you load the English module you can also use the $PROCESS_ID or the $PID variable names for the same thing.


2 Answers

The process number of the current process is in $$. Use open3 instead of system to receive the process number of the child process.

like image 89
daxim Avatar answered Sep 22 '22 08:09

daxim


By the time system returns, the spawned process will have already exited, leaving its pid as an interesting historical reference—useful for log analysis, for example.

system LIST
system PROGRAM LIST

Does exactly the same thing as exec LIST, except that a fork is done first and the parent process waits for the child process to exit

If you want the pids of both script.pl and newscript.pl, fork and manage their lifetimes yourself. With more specific information about the problem you’re tackling, we could give you more specific suggestions.


To block other instances of a program, a common technique is to use the operating system’s facility for cooperative locking: at startup, attempt to lock a certain file. If successful, your program knows it’s the only one. Otherwise, another process already has the lock, so the new process exits. See below for an example.

#! /usr/bin/env perl

use strict;
use warnings;

use Fcntl qw/ :flock /;
use File::Basename;
use POSIX qw/ strftime /;

$0 = basename $0;

my $LOCK = "$ENV{HOME}/.lock-$0";

sub logmsg {
  my($msg) = @_;
  my $t = strftime "%F %T", localtime time;
  warn "$0: $t - $$ - $msg\n";
}

sub take_lock {
  open my $fh, ">", $LOCK or die "$0: open $LOCK: $!";

  unless (flock $fh, LOCK_EX | LOCK_NB) {
    logmsg "failed to lock $LOCK; exiting.";
    exit 1;
  }

  $fh;
}

my $token = take_lock;
logmsg "processing...";
sleep 2 * 60;
logmsg "done.";

Note that you must keep the filehandle returned from take_lock open while control is inside your critical section. The code above treats it as an opaque token. When your program exits, Perl closes the filehandle, which releases the lock automatically. What you don’t want to do is call take_lock in void context because that would discard your lock.

like image 40
Greg Bacon Avatar answered Sep 22 '22 08:09

Greg Bacon