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

SOLVED This is terribly annoying me. I'm only a perl apprentice, so don't expect too much ;-)

My Question now is: How do I call an external program with the system() func from a CGI-Script? I am uploading an MS Excel file and want it to be parsed by an external program which will insert it into a database quietly. The shell call for this would be:
ccupdate_db -ExcelFile /var/www/cgi-bin/upload/test.xls -q

here's the code (with debug stuff):

# Variables alsway hold the full path to the file mentioned my $ccupdate_db = "/var/www/cgi-bin/ccupdate_db"; my $excel_file = "$uploadedfile"; #i.e. "/var/www/cgi-bin/upload/test. +xls" my @args = ($ccupdate_db, "-ExcelFile $excel_file", "-q"); my $exit_code = system(@args); print "Exit Code: ".$exit_code/256."<br>"; print "Error: ".$!:"<br>"; print "ARGS: "; foreach my $val (@args) { print $val." "; }
This will output:
Exit Code: 2
Error: Bad file descriptor
Args: /var/www/cgi-bin/ccupdate_db -ExcelFile /var/www/cgi-bin/upload/test.xls -q


WHY??? If I copy the outputted lines to the shell, all works fine...

Replies are listed 'Best First'.
Re: The System function and its usage
by mr_mischief (Monsignor) on Sep 28, 2007 at 14:58 UTC
    Instead of this:
    my @args = ($ccupdate_db, "-ExcelFile $excel_file", "-q");
    Try this:
    my @args = ($ccupdate_db, "-ExcelFile", "$excel_file", "-q");
    Anything that's separated by spaces is pretty much a separate argument as far as your OS is concerned, even though logically you have a sub-argument to an argument.

    Also, make sure your web server has read access to the file. For that matter, since you're also uploading it, make sure it's getting written. Many CGI vs. command-line issues are related to permissions.

Re: The System function and its usage
by jettero (Monsignor) on Sep 28, 2007 at 14:58 UTC

    You're passing "-ExcelFile $excel_file" as a single argument to the execvp() call (if that's even applicable under win32). You can either pass a single string to system() or you must pass each argument as a single string... my @args = ($ccupdate_db, "-ExcelFile", $excel_file, "-q")

    Additionally, check the server error logfile. It's likely your command line app spewed forth errors into that file when it failed to work as expected.

    -Paul

      You are my personal superheroes! In the log I found a permission denied statement... which belonged to the other program I wanted to call. This program wanted to write a log into a write-protected dir... Thank you so much!
        Also note that the shell environment (i.e., envars) under which you make your system call will be very different between interactive command line and CGI.
Re: The System function and its usage
by toolic (Bishop) on Sep 28, 2007 at 15:06 UTC
    In addition to what mr_mischief and jettero said, you tricked yourself into believing that @args was doing what you wanted in your foreach/print loop. To see what I mean, add this code:
    print @args; print "\n";
    This will print out:

    /var/www/cgi-bin/ccupdate_db-ExcelFile /var/www/cgi-bin/upload/test.xls-q

Re: The System function and its usage
by blue_cowdawg (Monsignor) on Sep 28, 2007 at 15:07 UTC
        WHY??? If I copy the outputted lines to the shell, all works fine...

    At the risk of seeming thick, what errors are showing in your web server logs?

    The last line of your post is very telling to me. If a script works fine from the command line and doesn't from the CGI environment then the first thing I think of being wrong is permissions. Web servers (and hence the CGI scripts which become child processes of the web server process) tend to run as an unprivileged user.

    You haven't specified what operating system this is all running under but based on your file path notations I'm going to presume it is a flavor of Unix or Linux.

    Quite often web servers are run as the user id "nobody" which typically has no permissions on the system at all. Other implementations include using the userid of "apache", "www" or "web" or some derivation thereof.

    Check the ownership and permissions of both the upload directory in question and the file in question. Check what userid your web processes are running as. Make sure the permissions of the files and directories match the userid that the web processes are running as.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg