Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

system command erroneously states 'can't spawn <executable>'

by Sandy (Curate)
on Feb 24, 2005 at 22:19 UTC ( [id://434282]=perlquestion: print w/replies, xml ) Need Help??

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

Also see system command can't spawn cmd.exe

On more than one occasion, I have received the dreaded dos message Can't spawn <executable> after executing a perl command

my $ret = system(executable);

Sometimes the error message refers to "cmd.exe" even though I am trying to run another executable.

Well, one time (once too many) I knew that since my perl program had been happily working for many months, and now wasn't (after i modified the executable in question), it had to do with the executable, not with perl.

So, with a little experimentation, I have discovered that this system error occurs if the executable returns with a negative return code (at least on WinNT, and definately not on solaris).

It only happens when called from a perl script, and not from a

perl -e "..."
, and only when using the system command.

For example, with the following c-code program

#include "stdio.h" int main(int argc, char* argv[]) { int rc,i; printf("Hello World!\n"); if (argc > 1) { sscanf(argv[1],"%d",&rc); printf("Returning with code %d\n",rc); i = 10/rc; printf ("i = %d\n",i); return rc; } printf ("Return with code 0\n"); return 0; }
and the following perl code
#!/usr/bin/perl use strict; use warnings; my $input = shift || ""; my $cmd = "rcs_c.exe $input"; print "\n\n--- Executing cmd '$cmd' via system\n\n"; my $rc = system $cmd; print "--- Execution finished with return code: ",$rc>>8,"\n"; print "\n\n--- Executing cmd '$cmd' via backticks\n"; my $return = `$cmd`; print "--- Execution finished, and returned:\n\n$return\n"; print "\n\nbye...\n"; exit $rc>>8;
I can execute my script passing a parameter which in turn will be passed to the c-code. This parameter will be used as the return code from within the c-program.

Note that with a return code of zero or a positive number, there is no error message, but when the return code is negative (any negative number gives same return code), the c-executable still executes, but prints that annoying error on the screen.

Important note: Even though there is that annoying message that initially had me convinced something was very wrong, it turned out (at least in these cases) that my executable executed quite happily, and everything was a-ok.

Note also that this does not occur if I execute the system command via a perl -e "system(...)"

D:\rcs>perl rcs.pl 0 --- Executing cmd 'rcs_c.exe ' via system Hello World! Return with code 0 --- Execution finished with return code: 0 --- Executing cmd 'rcs_c.exe ' via backticks --- Execution finished, and returned: Hello World! Return with code 0 bye... D:\rcs>perl rcs.pl -1 --- Executing cmd 'rcs_c.exe -1' via system Hello World! Returning with code -1 i = -10 Can't spawn "rcs_c.exe -1": No error at rcs.pl line 8. --- Execution finished with return code: 255 --- Executing cmd 'rcs_c.exe -1' via backticks --- Execution finished, and returned: Hello World! Returning with code -1 i = -10 bye... D:\rcs>perl -e "system(qq(rcs_c.exe -1))" Hello World! Returning with code -1 i = -10
For those with sharp eyes, please note that there was no difference between
my $rc = system ("$cmd");
and
my $rc = system ($cmd);
and
my $rc = system $cmd;
UPDATE: As noted by ikegami, the warning does occur from the perl command line if I 'use warnings'.

Also: If I change the system command to redirect the output, it does indeed say 'Can't spawn "cmd.exe"'.

So, now that I know that a negative return code from an executable causes DOS to barf up an error/warning message, I can deal with it.

Replies are listed 'Best First'.
Re: system command erroneously states 'can't spawn <executable>'
by ikegami (Patriarch) on Feb 24, 2005 at 23:06 UTC
    Sometimes the error message refers to "cmd.exe" even though I am trying to run another executable.

    The shell (cmd.exe) is needed to parse the command line in some instances. This is definitely the case when redirection (>, < or |) is used. There may be other cases too.

    perl uses environment variable PATH to locate cmd.exe. It should use environment variable COMSPEC to which shell and where it's located, but it doesn't.

    Note also that this does not occur if I execute the system command via a perl -e "system(...)"

    Maybe cause you didn't turn on warnings, and you had warnings on in your script?

    Historic tidbit that may be relevant: Negative return codes didn't use to be valid in DOS. The return value argument of the exit program system call was an unsigned byte. This may have changed in Windows.

      Hi

      According to the source (win32.c), COMSPEC isn't called ...
      /* we don't use COMSPEC here for two reasons: * 1. the same reason perl on UNIX doesn't use SHELL--rampant and * uncontrolled unportability of the ensuing scripts. * 2. PERL5SHELL could be set to a shell that may not be fit for * interactive use (which is what most programs look in COMSPE +C * for). */
      I don't really understand the second point ...

      - jim
Re: system command erroneously states 'can't spawn <executable>'
by RazorbladeBidet (Friar) on Feb 24, 2005 at 23:53 UTC
    Adding to the above - if that portion of the program isn't critical, or you want to just log it, try wrapping it in an eval and checking the error.
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: system command erroneously states 'can't spawn <executable>'
by jimbojones (Friar) on Feb 25, 2005 at 16:49 UTC
    Hi

    The issue, and the error message, aren't from DOS, they're from Perl source code in win32.c. If the system returns a negative error code, the Perl C code spits out that warning, and sets $? to 255 * 256, which you bit shift down to 255.

    I don't know how (yet) to raise a bug issue with the Perl source, but perhaps that error can be checked for which version of the windows perl is running on, and only return it for Windows OSs that don't support negative error codes.

    - j

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://434282]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-25 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found