2

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???

Mike Cooper
  • 1,065
  • 3
  • 13
  • 34

2 Answers2

4

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.

Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • 1
    Yes, the problem is that inside cron+perl the $PATH was not set to include /sbin where "lvs" is. So specifying "/sbin/lvs" fixes the problem. Thank you!!! – Mike Cooper Apr 09 '16 at 22:15
2

Try using absolute path to lvs:

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