hello everyone,

please i wrote a perl script to run a .bat file using the syntax

system 'file.bat'. this works fine when i run it from the command prompt. but i tried saving the script as .cgi and put it inside the cgi-bin so that i can call it from the browser but this is not working

i will really appreciate all suggestions on how i can achieve this. i want a perl script that can run the .bat file when called from a browser

Recommended Answers

All 12 Replies

The problem is probably that the file is not executable. Make sure that the web server user can execute the file. Is this windows or unix? Is this apache? I suspect that it has nothing to do with your perl file. It is probably either web server or system configuration. If you can run it from the command line, it is good for that user. However, the web server has to be able to execute it. Did you look in the server error logs to see what message you're getting?

hi mitchems below is the error i got. here is the cgi script i ran:
#!C:\Perl\bin\perl.exe
system 'jmeter -t';

when i saved this same script as .pl and run from command prompt. it launched that application but saving as .cgi and calling from a browser i got the error below.


Server error!
The server encountered an internal error and was unable to complete your request.

Error message:
malformed header from script. Bad header=Usage: jm.cgi

If you think this is a server error, please contact the webmaster.

Error 500
localhost
30/08/2010 14:40:48
Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1

Yes, you have to print out a valid HTTP header.

Try adding:

print "Content-type: text/html\n\n";

Before you call the batch file and see if that works.

yeah i already added that to it but it is not working.

Have you tried just running a script that puts out a header? I believe that you need to associate cgi with perl, so that perl is run. The first line on windows doesn't do anything. If you use a .pl extension that might work. However, if you want to use a cgi extension, you have to associate .cgi with the perl executable in the apaache httpd.conf file. Windows/Apache doesn't know to use perl to run that file.

yeah i already added that to it but it is not working.

You haven't said yet whether any of your other cgi scripts run on your server. What happens when you run a really simple one? Such as:

#!/usr/bin/perl
#hello.cgi
use strict;
use warnings;

print "Content-type: text/html\n\n";

print "Hello, world!\n";

If that runs without error, then we can look at what is different about the bat file-calling script that doesn't work.

hi guys, the code below works fine:
#!C:\Perl\bin\perl.exe
print "Content-type: text/html\n\n";
print "HELLO \nWORLD!\n";

i am using windows system, and i called using the url http://localhost/cgi-bin/hello.cgi. so this works fine.

please tell how i will associate cgi with perl executable in the httpd.conf, aalthough i have examined this critically.

To d5e5: the script below works fine wen i ran from command prompt, the problem is calling it from a browser.
#!C:\Perl\bin\perl.exe
print "Content-type: text/html\n\n";

system "jmeter";

Perhaps you should specify the entire path to the .bat file. This works for me:

#!c:/perl/bin/perl.exe
print "Content-type: text/html\n\n";

print "hello<br>";

system("D:\\Program Files\\Apache Software Foundation\\Apache2.2\\cgi-bin\\echo.bat");

echo.bat is:

echo "hello"

BUT, I had to name the cgi script t.pl for it to work. I don't have the time to mess around with the apache configuration.

Output in the browser:

hello
D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin>echo "hello" "hello"

hi guys, the code below works fine:
#!C:\Perl\bin\perl.exe
print "Content-type: text/html\n\n";
print "HELLO \nWORLD!\n";

i am using windows system, and i called using the url http://localhost/cgi-bin/hello.cgi. so this works fine.

please tell how i will associate cgi with perl executable in the httpd.conf, aalthough i have examined this critically.

To d5e5: the script below works fine wen i ran from command prompt, the problem is calling it from a browser.
#!C:\Perl\bin\perl.exe
print "Content-type: text/html\n\n";

system "jmeter";

Since http://localhost/cgi-bin/hello.cgi runs OK on your server it seems to me your server already associates cgi with perl, so maybe you don't have to touch httpd.conf. I have to do something else right now but I hope to get back online this afternoon and have another look at this.

What I suspect is that the print buffering, if any, is causing a problem. Maybe the output from the called bat file is somehow preceding the "Content-type: text/html\n\n" in the print buffer. If the output arrives at the browser in the wrong sequence, that can cause the error you are getting. In the meanwhile, have a look at http://perl.plover.com/FAQs/Buffering.html

The following passage looks interesting: "The content-type and the title are printed to STDOUT, which is buffered, but [...some error...] message is printed to STDERR, which isn't buffered. Result: The content-type and title are buffered. Then the error message comes out, and then, when the program exits, the STDOUT buffer is flushed and the content-type and title come out at last. The server was expecting to see the Content-type line right away, gets confused because it appears to be missing, and reports an error."

hi mitchems
in response to my thread. did you call the echo.bat from comand prompt or from a browser because i dont seems to see the url you anywhere in the post.
i will also like to emphasize that the jmeter.bat is suppose to start an application (jmeter gui). this works perfectly when i ran the script from the command prompt. but i want to do this from a browser. that is why am going through this stress so that if it works from a browser i can attach this command to a button a

You could try running the following, which incorporates one of solutions recommended in that article about buffering

#!C:\Perl\bin\perl.exe
#test.cgi
use strict;
use warnings;
#"The CGI::Carp module will arrange that fatal error messages
#are delivered to the browser with a simple prefabricated HTTP header,
#so that the browser displays the error message properly."
use CGI::Carp 'fatalsToBrowser';

print "Content-type: text/html\n\n"; #If no error, print your header anyway.
print "Try to run system 'jmeter -t'";
system 'jmeter -t';

I can't test it because my server platform is Linux, not Windows.

Apache have a very limited permission to execute programs to affect the system directly.

Please post your code. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.