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

A buddy of mine is trying to run a perl script as a cgi in his school cgi-bin. He's on Sun Solaris and i was hoping someone knew something i didn't (which is almost ALWAYS the case:). I can ssh in and run the script fine from the command line. I found perl (whereis perl) on his machine, so it's not that. A simple hello world script returns internal server errors. His log sez "Premature end of script headers." I chmoded the file to 0755. Uploaded as ascii. Even tried creating a new cgi in pico just to be sure. Here's the code which will not run properly through a browser on Sun Solaris. Am i missing something? Thanks for reading!
#!/usr/bin/perl use strict; print "Content-type:text/html\n\n"; print "hello";
jtrue

Retitled by davido.

Replies are listed 'Best First'.
Re: CGI: Premature end of script headers
by fglock (Vicar) on Jan 20, 2005 at 02:18 UTC
Re: CGI: Premature end of script headers
by Errto (Vicar) on Jan 20, 2005 at 02:06 UTC
    If he's running it from a personal cgi-bin directory, then quite likely it's running under suexec, which is extremely finnicky about permissions. In particular, the permissions in his cgi-bin directory (and his home directory, which I assume is cgi-bin's parent) should probably be set to 711 (though 755 might be ok). To see If the script really is running and it's just generating an error, add
    use CGI::Carp qw/fatalsToBrowser/;
    near the top of the script.
Re: CGI: Premature end of script headers
by Tanktalus (Canon) on Jan 20, 2005 at 00:15 UTC

    Given that it works fine using Apache on Linux, I'd have to wonder if you can get a look at the error_log for your server - that would help immensely in debugging the problem. I would hazard a guess and say that the system is not set up properly for CGI ;-)

Re: CGI: Premature end of script headers
by true (Pilgrim) on Jan 20, 2005 at 01:06 UTC
    this is the apache log output "Premature end of script headers."
    jtrue
      Really know nothing about Solaris but try adding "-w" to the first line, as in:
      #!/usr/bin/perl -w
      It sometimes helps when I shift between windows and linux OSs.
Re: CGI: Premature end of script headers
by ZlR (Chaplain) on Jan 20, 2005 at 08:22 UTC

    My guess : he's not using the correct path for perl in the shebang.

Re: CGI: Premature end of script headers
by atcroft (Abbot) on Jan 20, 2005 at 18:19 UTC

    Try inserting the following line between lines 2 and 3 of the code sample above:

    $| = 1;
    This will cause your output to be flushed immediately, rather than buffered (the default), which may help if an error is occurring and is arriving before the 'Content-type' header.

    HTH.