Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Unable to run batch file in CGI Script

by venkatesan_G02 (Sexton)
on Sep 23, 2009 at 12:43 UTC ( [id://796949]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I am trying to run a batch file which will start a server through my CGI Script. The Script is running fine if i run the program through command prompt but when i execute the CGI Script through a browser, the batch file is not being executed.
#!C:/Perl/bin/perl.exe use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $cgi=new CGI; print $cgi->header(); print $cgi->start_html("Welcome"); print "<body bgcolor=#c0c0c0>"; system 'C:/BatchTest/test.bat'; print "Program ends"; print "</body>"; print $cgi->end_html();
The Program is print "Program ends" both in command prompt and in browser. But the batch file is not being executed when run through browser.

Any help will be appreciated!!!

Thanks

Replies are listed 'Best First'.
Re: Unable to run batch file in CGI Script
by almut (Canon) on Sep 23, 2009 at 12:54 UTC

    Try  system('C:/BatchTest/test.bat') == 0 or print "error: $!\n";

    (Update: maybe I should add that if system() fails to execute the command, it returns -1, and only then $! is guaranteed to hold a meaningful value... (so testing ... == -1 and print $! would be the proper way).  I suggested to check for a system() return value of zero, as then it would still print "error:" even in the rare case that the batch command should execute but not exit with zero (which typically means an error occurred on the command's side...) )

Re: Unable to run batch file in CGI Script
by VinsWorldcom (Prior) on Sep 23, 2009 at 13:11 UTC

    It depends on your version of IIS (I'm assuming IIS as you're running Windows Batch files based on your code example). Newer versions of IIS do not allow access to CMD.EXE which is needed to 'interpret' the batch file. This is a new 'security feature' that gave me issues too.

    I didn't deal too much with it, just rewrote my batch in Perl, but I understood the only way to get around it was to run the IIS service as "Administrator" - HIGHLY **NOT** RECOMMENDED!!!

      Hi,

      I am using Apache web server.
        I am stuck with the same problem did u find any solution for this
Re: Unable to run batch file in CGI Script
by Bloodnok (Vicar) on Sep 23, 2009 at 14:35 UTC
    Nothing you've written will cause your CGI script to die, hence, assuming your script generates nothing on STDOUT or STDERR, nothing will be output to the browser (via CGI::Carp::fatalsToBrowser). To get a textual representation of any errors, try something like...
    my @out = `C:/BatchTest/test.bat`; die "@out ($?)" if $? != 0;

    Check the Apache server logs - they may (or may not;-) provide some indication.

    Is Apache configured to 'recognise' the directory containing the target script i.e. C:/BatchTest ?

    If so, are the permissions on C:/BatchTest correct ?

    A user level that continues to overstate my experience :-))
Re: Unable to run batch file in CGI Script
by marto (Cardinal) on Sep 23, 2009 at 13:09 UTC
Re: Unable to run batch file in CGI Script
by ww (Archbishop) on Sep 24, 2009 at 02:15 UTC
    "...trying to run a batch file which will start a server through my CGI Script."
    Errrr....

    What server are you trying to start? What does your batch file look like?

    Clearly, you can't start the Apache server if you're using it already. "Restart" maybe, not "start." And if that's the case, the newly re-started Apache isn't going to know your script from a hole in the wall.

Re: Unable to run batch file in CGI Script
by venkatesan_G02 (Sexton) on Sep 23, 2009 at 13:58 UTC
    system ('C:/BatchTest/test.vbs') == 0 or print "Error: $!\n"; is returning the following error "Error: No such file or directory".


    But when i execute it through command prompt, its executing fine.
      No such file or directory

      Is your webserver running on the same machine that the file resides on?  (It's the webserver that runs the CGI script - and thus the system() command - not the browser — Just in case...)

        Yeah. Apache web server is running in the same machine. I even tested the program by placing the batch file in cgi-bin folder and htdocs folder but no use.
Re: Unable to run batch file in CGI Script
by venkatesan_G02 (Sexton) on Sep 23, 2009 at 15:07 UTC
    When i use, system('C:/Server/Apache/cgi-bin/test.bat') == -1 or print "Error: $!\n";

    The error log file has "Wed Sep 23 20:44:11 2009 error client 4.33.33.177 Wed Sep 23 20:44:11 2009 Run_Another_Prog_from_perl.cgi: Can't spawn "cmd.exe": No such file or directory at C:/Server/Apache/cgi-bin/Run_Another_Prog_from_perl.cgi line 15. ".


    I copy and pasted cmd.exe in both htdocs and cgi-bin folder but it's still not working.

      What version of Windows - Server 2003?

      This is *very* similar to the problem I described with IIS - CMD.EXE was not allowed to run by the web server (in my case IIS) even if it resided in the CGI-BIN directory and had proper permissions (everyone - full permissions).

      Is this a test system - isolated from the outside world? Try running the Apache service as admin and see if that fixes it - if so, you're experiencing the same issue I was. Obviously running as admin is *NOT* the solution, but it may help diagnose the anomoly you're seeing.

        Firstly i am having Windows XP. Secondly, i have admin rights in the system and so Apache should also should have admin rights. Thirdly, the system is connected to internet.
Re: Unable to run batch file in CGI Script
by venkatesan_G02 (Sexton) on Sep 23, 2009 at 16:13 UTC
    Hi, i finally made the CGI script to open the batch file using
    system('C:\\Server\\Apache\\cgi-bin\\test.bat');
    The problem now is its printing whatever is written in the batch file but not executing it.

    The error log says "Not enough storage is available to process this command.\r "
    What does this error mean?
      Maybe you can't start a batch file directly, maybe you have to say system('cmd.exe _path_to_batch_file');
        I tried by placing the test.bat file in the C:\Windows\system32 folder and changing thesystem command to "system('cmd.exe test.bat'); but i am getting "Premature ending of headers" error. But the same program is working when executed through command prompt.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-16 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found