943,172 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 1544
  • Perl RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 29th, 2010
0

how to use perl script to run executable file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotmandd is offline Offline
5 posts
since Aug 2010
Aug 30th, 2010
0
Re: how to use perl script to run executable file
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?
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Aug 30th, 2010
0
Re: how to use perl script to run executable file
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotmandd is offline Offline
5 posts
since Aug 2010
Aug 30th, 2010
0
Re: how to use perl script to run executable file
Yes, you have to print out a valid HTTP header.

Try adding:
Perl Syntax (Toggle Plain Text)
  1. print "Content-type: text/html\n\n";

Before you call the batch file and see if that works.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Aug 30th, 2010
0
Re: how to use perl script to run executable file
yeah i already added that to it but it is not working.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotmandd is offline Offline
5 posts
since Aug 2010
Aug 30th, 2010
0
Re: how to use perl script to run executable file
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.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Aug 30th, 2010
0

Does a simple "Hello World" script run on your server?

Click to Expand / Collapse  Quote originally posted by dotmandd ...
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:
Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2. #hello.cgi
  3. use strict;
  4. use warnings;
  5.  
  6. print "Content-type: text/html\n\n";
  7.  
  8. 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.
Last edited by d5e5; Aug 30th, 2010 at 5:30 pm.
Reputation Points: 153
Solved Threads: 140
Master Poster
d5e5 is offline Offline
735 posts
since Sep 2009
Aug 30th, 2010
0
Re: how to use perl script to run executable file
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";
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotmandd is offline Offline
5 posts
since Aug 2010
Aug 31st, 2010
0
Re: how to use perl script to run executable file
Perhaps you should specify the entire path to the .bat file. This works for me:

Perl Syntax (Toggle Plain Text)
  1. #!c:/perl/bin/perl.exe
  2. print "Content-type: text/html\n\n";
  3.  
  4. print "hello<br>";
  5.  
  6. system("D:\\Program Files\\Apache Software Foundation\\Apache2.2\\cgi-bin\\echo.bat");

echo.bat is:

Perl Syntax (Toggle Plain Text)
  1. 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:

Perl Syntax (Toggle Plain Text)
  1. hello
  2. D:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin>echo "hello" "hello"
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Aug 31st, 2010
0
Re: how to use perl script to run executable file
Click to Expand / Collapse  Quote originally posted by dotmandd ...
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."
Reputation Points: 153
Solved Threads: 140
Master Poster
d5e5 is offline Offline
735 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: HTML form value into Perl
Next Thread in Perl Forum Timeline: How to open a web page using perl scripts





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC