Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pid from parent perl daemon?

Tags:

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.

like image 422
Alexandr Avatar asked Dec 05 '25 19:12

Alexandr


1 Answers

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]
like image 200
mpapec Avatar answered Dec 08 '25 12:12

mpapec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!