Search code examples
regexperlbackground-processpid

Get PID of new process


So I'm creating a process in Perl like this:

my $process = `nohup ./run > /dev/null 2>&1 &`;

Which returns something along the lines of

[1] 2905

How do I go about getting the process ID from this so later on in the script execution I can run something like:

exec("kill -9 $pid");

Here's what I've got so far:

/\[1\] ([0-9]+)/g

but it looks quite messy, is there any way to improve upon this regular expression? Will that regex always work? Is there any case where it wont be [1]?


Solution

  • how about

    @ar  = split(/\s+/, $process);
    $pid = $ar[1];