【问题标题】:Linux LVM lvs command fails from cron perl script but works from cron directlyLinux LVM lvs 命令从 cron perl 脚本失败,但直接从 cron 工作
【发布时间】:2016-07-30 22:48:47
【问题描述】:

我正在尝试在 perl 脚本中运行“lvs”来解析其输出。

my $output = `lvs --noheadings --separator : --units m --nosuffix 2>&1`;
my $result = $?;
if ($result != 0 || length($output) == 0) {
    printf STDERR "Get list of LVs failed (exit result: %d): %s\n",
    $result, $output;
    exit(1);
}
printf "SUCCESS:\n%s\n", $output;

当我从终端窗口运行上述脚本时,它运行良好。如果我通过 cron 运行它会失败:

Get list of LVs failed (exit result: -1): 

注意缺少任何输出(stdout + stderr)

如果我直接在 cron 中运行相同的 "lvs --noheadings --separator : --units m --nosuffix" 命令,它运行并输出就好了。

如果我修改 perl 脚本以使用 open3(),我也会遇到同样的失败,但没有输出。

如果我在 lvs 命令中添加“-d -d -d -d -d -v -v -v”以启用详细/调试输出,我会看到当我从终端运行 perl 脚本时,但没有通过 cron/perl 运行时的输出。

我在 RHEL 7.2 上使用 /usr/bin/perl (5.16.3) 运行它

有什么建议吗???

【问题讨论】:

    标签: linux perl cron lvm


    【解决方案1】:

    根据perldoc system,“返回值-1表示程序启动失败或wait(2)系统调用出错(检查$!原因)。”所以没有输出的原因是lvs没有成功启动。

    鉴于与 cron 相关的问题的常见性质,我想说它无法运行的最可能原因是它不在 cron 使用的 $PATH 上。尝试指定完整路径,如果这不起作用,请检查$! 以获取操作系统的错误消息。

    【讨论】:

    • 是的,问题是在 cron+perl 内部,$PATH 没有设置为包含“lvs”所在的 /sbin。所以指定“/sbin/lvs”可以解决问题。谢谢!!!
    【解决方案2】:

    尝试使用lvs的绝对路径:

    my $output = `/sbin/lvs --noheadings --separator : --units m --nosuffix 2>&1`;
    

    【讨论】:

      猜你喜欢
      • 2013-09-01
      • 1970-01-01
      • 2011-12-21
      • 2011-07-22
      • 1970-01-01
      • 2012-07-21
      • 2015-02-26
      • 2011-05-19
      • 2019-10-16
      相关资源
      最近更新 更多