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

is it possible to call a system command from a CGI script. sorry i'm a perl newbie and i keep asking questions like this... but is it possible to run a system command from your CGI browser script... like exec"notepad"
  • Comment on running system commands from a CGI script

Replies are listed 'Best First'.
Re: running system commands from a CGI script
by FloydATC (Deacon) on Jul 24, 2010 at 14:21 UTC
    Note that you most likely want do use system() and not exec() in your example. That is, unless the Windows program in question takes care of sending whatever output you wanted to send.

    Compare perldoc -f exec and perldoc -f system for more details.

    -- Time flies when you don't know what you're doing
      i tried running a..... system('notepad');.... in my CGI just to test and it takes forever then gives this..... HTTP Error 502.2 - Bad Gateway. what am i doing wrong?

        This is what happens:

        notepad.exe wants to create a window, but Windows blocks that because services don't have a desktop to create windows on. So notepad.exe sits there, idling, waiting for an API call to return that will never return. Your CGI has started notepad.exe via system, so it sits there and waits for notepad.exe to terminate. It won't, so your CGI won't, either. After some time, the webserver decides that your CGI is stuck, cuts all possible connections to that CGI and returns the error page to your browser.

        This is what you are doing wrong:

        You are starting a program designed to run on a desktop, showing a graphical user interface, from a service.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: running system commands from a CGI script
by Anonymous Monk on Jul 24, 2010 at 10:30 UTC
    Of course it is possible

    .. like exec"notepad"

    No. notepad is a GUI program and CGIs only runs CLI programs.

      Actually, a CGI program doesn't care. I've seen CGI programs that start up xterms. It does of course the GUI program to know on which display it should appear, and it certainly does help if there's a human to interact with the GUI application.

      Starting a GUI program from a CGI program isn't useful that often - but certainly not impossible.

        Thanks. Yup, CGI doesn't care, but notepad wont magically appear on the users computer :)
Re: running system commands from a CGI script
by ahmad (Hermit) on Jul 24, 2010 at 13:23 UTC

    Yes it's possible, but I do not recommended running a system commands (specially for GUI applications) on a windows machine as it's behaviour is unexpected (Your process might get stuck - You never know until you try it).