1

I've got some weird situation going on with a 3rd party perl script. To demonstrate:

my $rc = system("tar -C /app/src -xvf /app/package.tar.gz");

if ($rc != 0)
{
   print "Return code: $rc";  
}

Running tar -C /app/src -xvf /app/package.tar.gz directly on the shell works just fine, echo $? shows the exit status 0. All good.

Also, when running this small perl snippet the return is always -1. If I try something like:

my $rc = system("ls");

if ($rc != 0)
{
   print "Return code: $rc";  
} 

...the return code will also be -1.

I don't have much knowledge in perl, so I wonder what could possibly trigger this behaviour.

The documentation doesn't mention anything in particular regarding this (http://perldoc.perl.org/functions/system.html).


Solution:

Fetching $!to get some more details about the error:

my $rc = system("tar -C /app/src -xvf /app/package.tar.gz");

if ($rc != 0)
{
   print "Return code: $rc\n";
   print "Reason: $!";  
}

Returned:

Return code: -1
Reason: No child processes

Applying $SIG{'CHLD'} = 'DEFAULT'; fixed the issue:

$SIG{'CHLD'} = 'DEFAULT';

my $rc = system("tar -C /app/src -xvf /app/package.tar.gz");

if ($rc != 0)
{
   print "Return code: $rc\n";
   print "Reason: $!";  
}

Some details from: What's the difference between various $SIG{CHLD} values?

Romero Junior
  • 243
  • 3
  • 11
  • I can imagine issues with the first case but the second one is strange. What system are you on? Do your Perl programs normally run ok -- or is this the first one you tried? If you redirect output to a file (`system('ls > ls.txt')` or such) do you get the correct file after this? If you use backticks instead, `my $ret = qx(ls);` what do you get in `$ret`? – zdim Mar 28 '18 at 19:33
  • What was the value of `$SIG{CHLD}`? Why wasn't the value change [local](http://p3rl.org/local)ized? – choroba Mar 29 '18 at 09:00
  • @choroba what do you mean by "Why wasn't the value change localized?" $SIG{CHLD} was previously undefined, which defaults to `IGNORE` (as far as I understood the documentation). – Romero Junior Mar 29 '18 at 09:25
  • Ah, OK. The remaining question is why Perl fails to wait() for the `system`. – choroba Mar 29 '18 at 11:43
  • Nice that you dug into it and updated but note that this doesn't at all explain the strange behavior that you report in the second snippet. The fact that installing a signal handler (and to default!) "fixes" it is good diagnostics but there is something seriously off there. – zdim Mar 29 '18 at 19:50

1 Answers1

4

Right in the documentation you linked to (emphasis mine):

Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).

choroba
  • 231,213
  • 25
  • 204
  • 289