反引号操作符返回状态码5(或1280)

时间:2012-02-27 09:13:19

标签: perl backticks

我需要替换radius autentification脚本(用于PPPoE连接)。这是一个全天候运行的大量使用的服务,所以为了测试新脚本,我在原始脚本的开头注入一个小代码来分叉一个调用新脚本的新进程(如果新的脚本没有破坏的危险)脚本错误)。这是注入的代码:

$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是什么意思? 如何在反引号中调试命令以找出出错的地方?

1 个答案:

答案 0 :(得分: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)中捕获命令的STDOUT和STDERR,并将其打印在日志文件中。

现在我在日志文件中找到了这条消息,发现了什么错误:

[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)

希望这会帮助别人,我会告诉你我的两个错误是什么(如果我错了请纠正我):

  1. 我没有完整路径加载了一个lib。
  2. 当我手动测试新脚本时,我只是从它所在的同一目录中调用了他。