2
votes

I am trying to run "lvs" in a perl script to parse its output.

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;

When I run the above script from a terminal window it runs fine. If I run via cron it fails:

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

Note the lack of any output (stdout + stderr)

If I run the same "lvs --noheadings --separator : --units m --nosuffix" command directly in cron, it runs and outputs just fine.

If I modify the perl script to use open3() I also get the same failure with no output.

If I add "-d -d -d -d -d -v -v -v" to the lvs command to enable verbose/debug output I see that when I run the perl script from terminal, but there is no output when run via cron/perl.

I'm running this on RHEL 7.2 with /usr/bin/perl (5.16.3)

Any suggestions???

2

2 Answers

3
votes

According to perldoc system, "Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason)." So the reason there's no output is because lvs isn't being started successfully.

Given the usual nature of cron-related problems, I'd say the most likely reason it's failing to run would be that it's not on the $PATH used by cron. Try specifying the full path and, if that doesn't work, check $! for the operating system's error message.

1
votes

Try using absolute path to lvs:

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