Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

system() or exec() in CGI scripts

by LupoX (Pilgrim)
on Sep 09, 2003 at 10:47 UTC ( [id://289977]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Lovers,

Following situation:
On a XP machine I am running a CGI script (Apache Webserver). I want to remotely open a Internet Explorer window.
I tried to simply call iexplorer.exe via system() and also via exec() inside the CGI script.
The behavior that occurs is that a process "iexplorer.exe" does start on the machine but no browser window does open.
After that I tried to call the iexplorer.exe indirectly via a batch file called by the CGI. That also does not work the way I would expect it.

Where is my fault?

Thank You for all comments...
Georg -> perlmonk@viot.de

Replies are listed 'Best First'.
Re: system() or exec() in CGI scripts (use Win32)
by grinder (Bishop) on Sep 09, 2003 at 11:47 UTC

    fork and exec, despite the heroic efforts of a number of people, don't really work on Win32 platforms. You're much better off using Perl's Win32 interface. Something like the following will get you on your way:

    use Win32; use Win32::Process; my $ie; Win32::Process::Create( $ie, 'C:/Program Files/Internet Explorer/iexplore.exe', 'http://localhost/foo.html', 0, NORMAL_PRIORITY_CLASS, '.' ) or print Win32::GetLastError();

    Needless to say, this won't run on Unix, but if you are careful you can encapsulate this, and do the Right Thing according to platform.

      Thank you this works perfectly! I spuriously trusted the built in function more than any module.
      In German I would say: "Aus Fehlern wird man klug, drum ist einer nicht genug" (What meens something like "From errors you get smart so one ist not enough")

      They work fine for me? Care to elusidate?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: system() or exec() in CGI scripts
by Abigail-II (Bishop) on Sep 09, 2003 at 10:55 UTC
    system and exec do two different things. system starts up another process, waits for it to finish, and then continues with the rest of the original process. exec replaces the current process with a new one.

    However, if explorer.exe starts on the machine, but doesn't exhibit the wanted behaviour, it's not a matter of system vs exec. In fact, it's unlikely to be a Perl problem. What happens if you run the command from the command line? What happens if you run the command from a CGI program written in a different language than Perl? Could it be that the webserver sets up an environment that iexplorer.exe doesn't like?

    Abigail

      If I start the perl script local the browser window does open. Also the batch file does work. The only difference to the CGI script ist that the explorer window does open and when starting via CGI it does not open.

        Then it's a CGI issue, and not a Perl issue. Your server sets up a different environment (variables, UID, permissions, limits) than you have from the command line. At least one of these differences prohibits your exe file to behave differently.

        Abigail

Re: system() or exec() in CGI scripts
by Roger (Parson) on Sep 09, 2003 at 12:08 UTC
    This is because the perl CGI script on your web server is run by mod_perl module, not perl. There is a specific method for starting a new process in apache CGI, which would look somewhat like the following:

    use Apache::SubProcess (); $command = "C:\\Windows\\iexplorer.exe"; Apache::SubProcess::spawn_proc_prog(undef, $command);
Re: system() or exec() in CGI scripts
by bm (Hermit) on Sep 09, 2003 at 11:09 UTC
    I want to remotely open a Internet Explorer window.

    Are you saying that you would like a new browser window to open on the user's computer that is running your CGI script? If so, you are chasing a red herring. Executing iexplorer.exe will start a new browser window on the server. You probably want it to start on the client.
    --
    bm

      No no I want to start the browser on the server where the CGI is running. The client should only invoke this process.

        It is probably not in your path. Try (assuming a default installation location):

        system('C:\Program Files\Internet Explorer\IEXPLORE.EXE');

        See Abigail-II's response above if you want your script to exit straight after IE is launched.
        --
        bm

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found