2012-02-27 72 views
1

我需要替換半徑autentification腳本(用於PPPoE連接)。這是一個24/7全天候運行的服務,因此爲了測試新腳本,我在原始腳本的開始處注入了一小段代碼,以分叉一個稱爲新腳本的新進程(如果新腳本沒有破壞的危險腳本失敗)。這是注入代碼:反撥操作員返回狀態碼5(或1280)

$pid = fork(); 
if($pid == 0) 
{ 
    my $now_string = POSIX::strftime "%a %b %e %H:%M:%S %Y", localtime; 
    open(FILE,">>/new_log_file.log"); 
    `/new_script_path/new_script.pl @ARGV`; 
    if ($? == 0) 
    { 
     print FILE "[".$now_string."] Chiled executed succesfuly\n"; 
    } elsif($? == -1) 
    { 
     print FILE "[".$now_string."] FAILED to execute\n"; 
    } elsif($? & 127) 
    { 
     printf FILE "[".$now_string."] child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; 
    } else 
    { 
     printf FILE "[".$now_string."] child exited with value %d\n", $? >> 8; 
    } 
    close(FILE); 
    exit; 
} 

不幸的是這未能執行新的腳本,並在日誌文件中我有這樣的信息:

"[Mon Feb 27 09:25:10 2012] child exited with value 5" 

不會偏移位,$的價值?是1280;

如果我手動調用它,新腳本會按預期工作。

狀態碼5是什麼意思? 如何在反引號中調試命令以找出發生了什麼問題?

回答

2

經過大量的搜索之後,我終於找到了調試它的方法。看看新的代碼:

my $now_string = POSIX::strftime "%a %b %e %H:%M:%S %Y", localtime; 
open(FILE,">>/new_log_file.log"); 
$output = `/new_script_path/new_script.pl @ARGV 2>&1`; 
if ($? == 0) 
{ 
    print FILE "[".$now_string."] Chiled executed succesfuly\n"; 
} elsif($? == -1) 
{ 
    print FILE "[".$now_string."] FAILED to execute\n"; 
} elsif($? & 127) 
{ 
    printf FILE "[".$now_string."] child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; 
} else 
{ 
    printf FILE "[".$now_string."] child exited with value %d (%d) [ %s ]\n", $? >> 8, $?, $output; 
} 
close(FILE); 

我所做的就是捕捉到反引號(文檔here)命令的輸出和錯誤,並在日誌文件打印出來。

現在我發現在日誌文件中此消息,並發現什麼是錯誤的:

[Mon Feb 27 09:40:41 2012] child exited with value 2 (512) [ Can't locate lib.pl in @INC (@INC contains: [long output removed]) at /new_script_path/new_script.pl line 30. 

我回答瞭如何調試它的問題,(奇怪的是,這個新代碼的狀態代碼是不是2 5)

在希望這會幫助別人,我會告訴你什麼是我的失誤2(請糾正我,如果我錯了):

  1. 我裝一個lib不完整路徑。
  2. 當我手動測試新腳本時,我只從它所在的相同目錄中調用他。