2014-10-16 61 views
2

我有一些測試代碼:如何從父perl守護進程獲取pid?

#!/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); 
} 

我需要的腳本test.pl知道他的PID。給他一個PID作爲參數將是理想的,但是在Init方法的論據中給他$ kid_pid的課程是不可能的。從文件讀取選項是不合適的。

現在我需要知道父腳本的PID。代碼my $ppid = Proc::Daemon::Init();不爲我工作,因爲我有循環腳本,他工作不正確。 $$不起作用,因爲Proc::Daemon::Init()有另一個PID。

+0

'exec_command => 「的perl /home/develop/test.pl $$」'然後'$ ARGV [0]''裏面test.pl' – 2014-10-16 11:35:12

+0

問題還是實際的,因爲我不知道怎麼弄來自子守護程序的父pid。從文件中讀取並不適合我。 – Alexandr 2014-10-16 13:49:31

+0

'$ ARGV [0]'在命令行中。 – 2014-10-16 13:52:03

回答

1

您可以使用$$變量獲得當前PID,使用getppid()可以獲得當前PID。

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

'getppid' in test.pl從'init --user'命令返回PID,不是我父母的PID,也不是他PID – Alexandr 2014-10-16 10:13:35

+0

'$$'爲我工作:) 現在我開始懷疑父PID還需要,如何得到它?) 腳本中的循環不允許我使用一些像這樣的'my $ ppid = Proc :: Daemon :: Init();' – Alexandr 2014-10-16 10:27:09

+0

'$$'現在不工作,因爲'Proc ::守護進程:: Init()'有另一個PID。 – Alexandr 2014-10-16 10:34:32