Alexandr
Alexandr

Reputation: 99

How to get pid from parent perl daemon?

I have some test code:

#!/usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;

my $daemon = Proc::Daemon->new;
Proc::Daemon::Init();
my $kid_pid = $daemon->Init(
    {   work_dir     => '/home/develop',
        pid_file     => 'pid.txt',
        exec_command => 'perl /home/develop/test.pl ',
    }
);
$| = 1;

while (1) {
    my $status = $daemon->Status( [$kid_pid] );
    if ( !$status ) {
        my $kid_pid = $daemon->Init(
            {   work_dir     => '/home/develop',
                pid_file     => 'pid.txt',
                exec_command => 'perl /home/develop/test.pl ',
            }
        );
    }
    sleep(5);
}

I need the script test.pl to know him PID. Give him a PID as an argument would be ideal, but in the arguments of Init method to give him $kid_pid ofcourse impossible. Option of reading from a file is not suitable.

And now I need to know PID of parent script. Code my $ppid = Proc::Daemon::Init(); dont work for me, because i have loop in script and he work incorrectly. $$ don't work too, because Proc::Daemon::Init() have another PID.

Upvotes: 3

Views: 1104

Answers (1)

mpapec
mpapec

Reputation: 50637

You can get current PID with $$ variable, and parent PID by using getppid().

perl -E 'say fork || getppid, " me:[$$]"'
7503 me:[7477]
7477 me:[7503]

Upvotes: 1

Related Questions