Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am executing a external command ( tar ) using backtick, while checking the return value it gives -1, and the $! gives no child process error. But the tar got created properly.

I tried the same with the system function also, but i get the same error.

Actually am new into the project, did not yet fully know/gonethrough/understood the framework - but referred other external command calls in the same project - and it is done the same way...

Machine is not having any problem: The machine/system which am using/testing this is not having any issue. Confirmed this by writing a sample program which creates a tar, $? gives 0 properly. So can somebody help me, what i have to look at ? Why do i get this error ?

I understand it is not possible to give solution by somebody else very easily. Actually am not looking for a solution, i spent a lot of time in fixing this... So i will be very happy to get atleast clues / guidance in finding out the issue. Thanks in advance.
  • Comment on External command execution - Strange Error - No child process

Replies are listed 'Best First'.
Re: External command execution - Strange Error - No child process
by toolic (Bishop) on Jul 25, 2011 at 16:12 UTC
    I am executing a external command ( tar ) using backtick, while checking the return value it gives -1
    Please show your code. What value do you expect backticks to return? Backticks return the output of the external command, which is different from the exit status of the command. See also system.
    and the $! gives no child process error
    Why are you checking $!? Maybe $? is more appropriate.
Re: External command execution - Strange Error - No child process
by zentara (Cardinal) on Jul 25, 2011 at 16:15 UTC
    You should show some of the external command strings that you are using. Tar can be very difficult to get the option pairings right.

    In lieu of you showing no code, here is a option string that took me awhile to get correct. You may have an option wrong, or maybe you want to add 2>&1>/dev/null to your options to quiet the output.

    Passing the commandline options to tar, for example only, not tested recently:

    system ('tar','-c','-XEXCLUDE_FILE','-M','-L50000',"-F ./backup-rotate +.pl $basename","-f$basename-0.tar",$dir);

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: External command execution - Strange Error - No child process
by ikegami (Patriarch) on Jul 25, 2011 at 18:35 UTC
    Are you perhaps using $SIG{CHLD} to reap the child? (Oops, just saw this was already mentioned. Kudos to zentara.)
Re: External command execution - Strange Error - No child process
by Anonymous Monk on Jul 25, 2011 at 16:25 UTC
    Sorry for not explaining it properly.

    When I meant, i checked the return/exit status. I checked the $? and found it as -1. If it is -1, we are supposed to check the $! which has the string - explanation of the error.

    The tar command I used is very simple, `tar -cf logs.tar logout.txt logerr.txt`

    Thanks for your reply.. Kindly help me in figuring out the issue.

      Presumably you shifted $? to get the exit code: $? >> 8 as documented in perlvar?
Re: External command execution - Strange Error - No child process
by onelesd (Pilgrim) on Jul 26, 2011 at 06:39 UTC

    The backtick operator returns the output of the command. The system function actually returns the exit value itself. If you want the exit value do this:

    my $exit_value = system($tar_command) ; die "tar failed: ".$exit_value / 256."\n" if $exit_value != 0 ;