簡體   English   中英

Linux LVM lvs命令在cron perl腳本中失敗,但直接在cron中工作

[英]Linux LVM lvs command fails from cron perl script but works from cron directly

我試圖在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 -d -v -v -v”以啟用詳細/調試輸出,我會看到當我從終端運行perl腳本時,但是運行時沒有輸出通過cron / perl。

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

有什么建議么???

根據perldoc系統 ,“ 返回值-1表示啟動程序失敗或wait(2)系統調用錯誤(原因是檢查$!)。 ”因此,沒有輸出的原因是lvs is'。無法成功啟動。

考慮到與cron相關的問題的通常性質,我想說它最有可能無法運行的原因是它不在cron使用的$PATH 嘗試指定完整路徑,如果不起作用,請檢查$! 有關操作系統的錯誤消息。

嘗試使用lvs絕對路徑:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM