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

I need to make a system call within a cgi script, and I am having some trouble. I know that I have done this before, but for some reason, when I make the call, things hang, and the debug just before my system call gets printed, but the debug after never gets printed.

I made s short .pl script, which works fine when I call "perl system.pl" at the command prompt.
system.pl:
#!/usr/local/bin/Perl/bin/perl.exe -w
my $SUCCESS = 0;

my $cmd = "notepad.exe \"error.log\"";
(system($cmd) == $SUCCESS) or print("Notepad failed: $!\n");

Now, within the confines of my cgi script (running win32 with apache), I am trying ($ME is defined to "filename.cgi:"): don_common'log_to_file($msg_log, "$ME about to make notepad call");
my $cmd = "notepad.exe \"error.log\"";
(system($cmd) == $SUCCESS) or don_common'log_to_file("notepad failed: $!\n");
don_common'log_to_file($msg_log, "$ME just made notepad call");

In my log file, I get the following output:
filename.cgi: about to make notepad call

Any ideas? I even tried to put a copy of notepad.exe in my cgi-bin directory. The error.log is also in the same directory.

Thanks,

-Kevin

Replies are listed 'Best First'.
Re: System call within cgi script
by Joost (Canon) on Jan 29, 2007 at 19:36 UTC
    Since the notepad is going to be opened on the webserver, there will very likely be nobody around to close it again.

    Or maybe the webserver doesn't have permission to start notepad, though I suspect that would give an error message.

    ps: using ' for package seperators is so very perl4. Use :: instead. Also all-lower-case package names are sort of reserved for pragmas.

    ie. use something like i.e. Don_Common::log_to_file(). Your fellow programmers will be less confused.

      I suspect Joost is right. I just tested the portion of the OP's code calling notepad (with WampServer), and I now have a phantom notepad process that I can see from the task manager even though the notepad GUI never appears. Since system does a fork and waits for the command to complete, the perl script is waiting for notepad to exit and return a value. since noone ever closes notepad, it never completes, hence the permenant hang.

      You could just fork and exec, but not only is that a sloppy solution, I assume you want the notepad window to actually display to the active user, right? Apache doesnt seem to want to allow that, and I have no idea how to get around it.

      __________
      The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
      - Terry Pratchett

        No, in this case fork/exec is complicating the matter. You'd want to go to basics with Win32::Process and use CreateProcess.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Well, the call I need to make isn't notepad, just an example to show that it isn't working. That, and everything is self-contained on one system -- it is for a documentation server for a large project.

      Anyway, good to know about the :: operator. I wish I knew before I put the ' in a million places.

      The error log says nothing. I do notice that I get an hourglass over the browser window I am trying to execute in. Is there any way to get any information as to what the heck is going on.
        Well, if the program you're trying to call is an interactive program (which notepad from your example is and most GUI programs are), then your issue is that the called program starts and drops into a "wait for user input" loop. since its called from a CGI script, however, Apache seems to prevent said interactive program from displaying in the active windows session, so it will never get any user input.

        diatolevi's suggestion of using Win32::Process is a step in the right direction, in that it starts the process and then exits the CGI script successfully. Unfortunately, it still does so under Apache, thus preventing the user from ever seeing the interactive program.

        __________
        The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
        - Terry Pratchett