You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by "michael watson (IAH-C)" <mi...@bbsrc.ac.uk> on 2004/02/17 12:21:09 UTC

[users@httpd] Help running perl cgi over apache

Hello

At first I thought this was something to do with perl, now maybe I think it is something wrong with my apache set up.

I am running Suse linux 8.2, Perl 1.5.8, Apache 1.3.28.

I have a perl script which fires off a shell script and then exits.  It does this as the shell script takes about 30 minutes to run, so perl doesn't want to wait around.  In it's most basic form the script looks like this:

#!/usr/bin/perl
print "Content-type: text/plain\n\n";
system("/bin/sh /path/to/command &");
print "Finished\n";

>From the command line, this works fine - the script fires off the process in the background and exits.  However, if I stick the exact same script in my cgi-bin directory, the perl script then waits for system() call to return i.e. my cgi script takes as long to run as the shell script does, i.e. it doesn't run it in the background, i.e. my server times out.

The fact that a script run on the command line works, but the behaviour changes over cgi makes me think maybe I am doing something wrong with my apache config.

Any ideas?

Thanks
Mick


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Jonas Eckerman <jo...@frukt.org>.
On Tue, 17 Feb 2004 07:47:02 -0500, Geoffrey wrote:

> >  print "Content-type: text/plain\n\n";

>  If you're expecting this to show up in a browser you want:
>  Content-Type: text/html

Most browsers have no problem at all displaying plain text. Actually, I have never encountered a browser that can't display plain text. He should only use text/html if he's actually sending HTML, if he's just sending plain text, text/plain is correct and may save the browser from doing unnessecary parsing.

Regards
/Jonas
-- 
Jonas Eckerman, jonas_lists@frukt.org
http://www.fsdb.org/


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Geoffrey <es...@3times25.net>.
michael watson (IAH-C) wrote:
> Hello
> 
> At first I thought this was something to do with perl, now maybe I
> think it is something wrong with my apache set up.
> 
> I am running Suse linux 8.2, Perl 1.5.8, Apache 1.3.28.
> 
> I have a perl script which fires off a shell script and then exits.
> It does this as the shell script takes about 30 minutes to run, so
> perl doesn't want to wait around.  In it's most basic form the script
> looks like this:
> 
> #!/usr/bin/perl
> print "Content-type: text/plain\n\n";
> system("/bin/sh /path/to/command &");
> print "Finished\n";

If you're expecting this to show up in a browser you want:

Content-Type: text/html

Also, you should look into the CGI module.

You shouldn't need to explicitly call the shell:

system("/path/to/command &");

should be sufficient as long as 'command' is executable.

You might look into the alternative solution of:

`/path/to/command &`;

Although I don't know that it will make much difference in this case.


> 
>> From the command line, this works fine - the script fires off the
>> process in the background and exits.  However, if I stick the exact
>> same script in my cgi-bin directory, the perl script then waits for
>> system() call to return i.e. my cgi script takes as long to run as
>> the shell script does, i.e. it doesn't run it in the background,
>> i.e. my server times out.
> 
> The fact that a script run on the command line works, but the
> behaviour changes over cgi makes me think maybe I am doing something
> wrong with my apache config.

Are you getting any errors in the error log?

Add:

#use diagnostics;

to the perl script and rerun it.  You'll get verbose errors to the log.

-- 
Until later, Geoffrey                     Registered Linux User #108567
Building secure systems inspite of Microsoft


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Richard Gration <ri...@zync.co.uk>.
michael watson (IAH-C) wrote:
 > Hello
 >
 > At first I thought this was something to do with perl, now maybe I
 > think it is something wrong with my apache set up.
 >
 > I am running Suse linux 8.2, Perl 1.5.8, Apache 1.3.28.
 >
 > I have a perl script which fires off a shell script and then exits.
 > It does this as the shell script takes about 30 minutes to run, so
 > perl doesn't want to wait around.  In it's most basic form the script
 > looks like this:
 >
 > #!/usr/bin/perl print "Content-type: text/plain\n\n"; system("/bin/sh
 > /path/to/command &"); print "Finished\n";
 >
 >> From the command line, this works fine - the script fires off the
 >> process in the background and exits.  However, if I stick the exact
 >> same script in my cgi-bin directory, the perl script then waits for
 >> system() call to return i.e. my cgi script takes as long to run as
 >> the shell script does, i.e. it doesn't run it in the background,
 >> i.e. my server times out.
 >
 > The fact that a script run on the command line works, but the
 > behaviour changes over cgi makes me think maybe I am doing something
 > wrong with my apache config.
 >
 > Any ideas?
 >
 > Thanks Mick

Hi,

If this is really all that your perl script does, then you might have
better luck not using perl at all. Simply write your script as a shell
script (change the shebang line if bash is not on your system):

#!/bin/bash
/path/to/command &
echo -ne "Content-Type: text/plain\r\n\r\n"
echo "Finished"

As long as the script is in a ScriptAlias directory it should run fine.
If it's not, you'll have to associate it's extension with the cgi-script
handler.

HTH
Rich


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Brian Dessent <br...@dessent.net>.
"michael watson (IAH-C)" wrote:

> I am running Suse linux 8.2, Perl 1.5.8, Apache 1.3.28.

I think you mean Perl 5.8.1.

> I have a perl script which fires off a shell script and then exits.  It does this as the shell script takes about 30 minutes to run, so perl doesn't want to wait around.  In it's most basic form the script looks like this:
> 
> #!/usr/bin/perl
> print "Content-type: text/plain\n\n";
> system("/bin/sh /path/to/command &");
> print "Finished\n";

If you want to run a program and have it continue running after the
parent exits, you need to detach (daemonize) it, not just run it in the
background.

Brian

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Nick Kew <ni...@webthing.com>.
>
> >  print "Content-type: text/plain\n\n";
>
> Shouldn't that be \r\n\r\n on a Linux system?

Not in CGI - which explicitly says \n is enough.

> >  system("/bin/sh /path/to/command &");

The answer to that has been in the CGI FAQ since '96.  Close your
file descriptors.

-- 
Nick Kew

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Jonas Eckerman <jo...@frukt.org>.
On Tue, 17 Feb 2004 16:09:45 +0000 (GMT), Nick Kew wrote:

>  That looks confused.

Not really, but I forgot to say that it's perl specific (it may be specific to some other languages as well of course).

>  I think you're using "\n" for newline.

As you say, \n is a newline. It's not a linefeed (LF) or a carriage return (CR). In newer versions of perl that means, IIRC, that the ouput of \n is platform dependant.

> >  Of course, the web server might be nice and handle this for the
> >  script

>  s/might/MUST/

>  If it doesn't then it's not CGI.

Ah. That's nice. Makes portability kind of easier. :-)

Regards
/Jonas

-- 
Jonas Eckerman, jonas_lists@frukt.org
http://www.fsdb.org/


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Nick Kew <ni...@webthing.com>.
> No, \r is not purely a DOS thing. Some protocols specify CRLF as the
  line termination, regardless of operating system.

Yep.

> IIRC the HTTP spec says each header line should be terminated by CRLF.

Yep - along with other RFC822-family protocols.

> On a DOS/Windows system \n should result in CRLF and should therefore be
 correct, but on a Linix or Unix system \n shpould result in LF wich means
  you have to specify \r\n to follow the HTTP standard.

That looks confused.  I think you're using "\n" for newline.

> Of course, the web server might be nice and handle this for the script
  so the poor programmer doesn't have to. :-)

s/might/MUST/

If it doesn't then it's not CGI.

-- 
Nick Kew

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Geoffrey <es...@3times25.net>.
Nick Kew wrote:
> On Wed, 18 Feb 2004, Geoffrey wrote:
> 
> 
>>The following is
> 
> 
> Broken (the headers are incompatible with the body).
> Clueless (for reasons already dealt with on this thread).
> 
> 
>>browser. Type in the following, save it to a file called first.pl, and
> 
> 
> The author has failed to look up the CGI FAQ or any reputable tutorial.
> 

That's terribly interesting considering the source:

http://httpd.apache.org/docs/howto/cgi.html#yourfirstcgiprogram

-- 
Until later, Geoffrey                     Registered Linux User #108567
Building secure systems inspite of Microsoft


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Nick Kew <ni...@webthing.com>.
On Wed, 18 Feb 2004, Geoffrey wrote:

> The following is

Broken (the headers are incompatible with the body).
Clueless (for reasons already dealt with on this thread).

> browser. Type in the following, save it to a file called first.pl, and

The author has failed to look up the CGI FAQ or any reputable tutorial.

-- 
Nick Kew

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re:apache web access based on previous url

Posted by Alex Moon <a....@mdx.ac.uk>.
Hi
I am trying to devise a way of controlling access to my apache 
2.048 website(website a) based on the previous website the visitor 
has viewed, blocking access if they have not previewed the 
originating website (website b). 
Is there anyway i can control this within the apache httpd conf file 
settings.   
Some guidance would be very much appreciated

Al

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Geoffrey <es...@3times25.net>.
Jonas Eckerman wrote:
> On Tue, 17 Feb 2004 07:49:22 -0500, Geoffrey wrote:
> 
> 
>>>> print "Content-type: text/plain\n\n";
> 
> 
>>> Shouldn't that be \r\n\r\n on a Linux system?
> 
> 
>> No.  \r is a dos thing, it's a wasted character in the UNIX world.
> 
> 
> No, \r is not purely a DOS thing. Some protocols specify CRLF as the
> line termination, regardless of operating system.
> 
> IIRC the HTTP spec says each header line should be terminated by
> CRLF. On a DOS/Windows system \n should result in CRLF and should
> therefore be correct, but on a Linix or Unix system \n shpould result
> in LF wich means you have to specify \r\n to follow the HTTP
> standard.
> 
> If I'm mistaken and the HTTP standard doesn't specify CRLF, then \n
> should be correct on a Unix system but not on a DOS/Windows system.

I'm too busy (or lazy??) to look up the RFC, but personal experience 
indicates it's not necessary on a Linux box.  I have noted that perl CGI 
populates each newline in this way though, so it's likely some truth 
behind it.  But, as I've noted, it does work with Apache on a Linux box 
without it.

It appears you may be correct though as little googling did produce the 
following:

The following is an example CGI program that prints one line to your 
browser. Type in the following, save it to a file called first.pl, and 
put it in your cgi-bin directory.

         #!/usr/bin/perl
         print "Content-type: text/html\r\n\r\n";
         print "Hello, World.";

Even if you are not familiar with Perl, you should be able to see what 
is happening here. The first line tells Apache (or whatever shell you 
happen to be running under) that this program can be executed by feeding 
the file to the interpreter found at the location /usr/bin/perl. The 
second line prints the content-type declaration we talked about, 
followed by two carriage-return newline pairs. This puts a blank line 
after the header, to indicate the end of the HTTP headers, and the 
beginning of the body. The third line prints the string ``Hello, 
World.'' And that's the end of it.

-- 
Until later, Geoffrey                     Registered Linux User #108567
Building secure systems inspite of Microsoft


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Jonas Eckerman <jo...@frukt.org>.
On Tue, 17 Feb 2004 07:49:22 -0500, Geoffrey wrote:

> > >  print "Content-type: text/plain\n\n";

> >  Shouldn't that be \r\n\r\n on a Linux system?

>  No.  \r is a dos thing, it's a wasted character in the UNIX world.

No, \r is not purely a DOS thing. Some protocols specify CRLF as the line termination, regardless of operating system.

IIRC the HTTP spec says each header line should be terminated by CRLF. On a DOS/Windows system \n should result in CRLF and should therefore be correct, but on a Linix or Unix system \n shpould result in LF wich means you have to specify \r\n to follow the HTTP standard.

If I'm mistaken and the HTTP standard doesn't specify CRLF, then \n should be correct on a Unix system but not on a DOS/Windows system.

Of course, the web server might be nice and handle this for the script so the poor programmer doesn't have to. :-)

>  I'm not quite sure what you mean by disown, but running it in the
>  background should permit the call to return.

There are different ways to run a process in the background, and the shell may act differently depending on this. In a bash shell "disown" means that the shell should no longer consider itself the owner of a background job (note: job, not process). When you leave an interactive bash shell that considers itself owner to a background job, it will tell you that it has background jobs instead of exiting (at least the first time you try).

I have no idea how sh handles jobs, or if it even has jobs. So of course I have even less knowledge of how sh would handle a job in a non-interactive shell.

>  By running it in the background, the process should release.

As I said, there are different ways to run things in the background. Using the & to tell the shell to run a background process might, depending on the shell, also tells it to do more than just create and forget a process.

Regards
/Jonas

-- 
Jonas Eckerman, jonas_lists@frukt.org
http://www.fsdb.org/


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Geoffrey <es...@3times25.net>.
Jonas Eckerman wrote:
> On Tue, 17 Feb 2004 11:21:09 -0000, michael watson (IAH-C) wrote:
> 
> 
>> print "Content-type: text/plain\n\n";
> 
> 
> Shouldn't that be \r\n\r\n on a Linux system?

No.  \r is a dos thing, it's a wasted character in the UNIX world.

> 
>> system("/bin/sh /path/to/command &");
> 
> 
> I may be totally mistaken here, but my first thought is that the CGI
> might not be able to exit until that job is finished. If that is the
> case, you could try to have the script disown the job and see if that
> helps. Or maybe get the stuff to run completely separately from the
> script.

I'm not quite sure what you mean by disown, but running it in the 
background should permit the call to return.  As I noted in a previous 
post, it may have something to do with the explicit use of /bin/sh. 
That shouldn't be necessary as long as /path/to/command is executable.

> 
> 
>> The fact that a script run on the command line works, but the 
>> behaviour changes over cgi makes me think maybe I am doing
> 
> 
> OTH you're not running it the same way when you're running it from
> the command line. The owner of the job doesn't try to exit before the
> job is finished when you run it from a command line unless you're
> also logging out before it's finished.

By running it in the background, the process should release.

-- 
Until later, Geoffrey                     Registered Linux User #108567
Building secure systems inspite of Microsoft


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help running perl cgi over apache

Posted by Jonas Eckerman <jo...@frukt.org>.
On Tue, 17 Feb 2004 11:21:09 -0000, michael watson (IAH-C) wrote:

>  print "Content-type: text/plain\n\n";

Shouldn't that be \r\n\r\n on a Linux system?

>  system("/bin/sh /path/to/command &");

I may be totally mistaken here, but my first thought is that the CGI might not be able to exit until that job is finished. If that is the case, you could try to have the script disown the job and see if that helps. Or maybe get the stuff to run completely separately from the script.

>  The fact that a script run on the command line works, but the
>  behaviour changes over cgi makes me think maybe I am doing

OTH you're not running it the same way when you're running it from the command line. The owner of the job doesn't try to exit before the job is finished when you run it from a command line unless you're also logging out before it's finished.

Regards
/Jonas
-- 
Jonas Eckerman, jonas_lists@frukt.org
http://www.fsdb.org/


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org