Forum Moderators: coopster & phranque

Message Too Old, No Replies

forking a cgi script

         

blakmonk

7:42 pm on Jul 6, 2004 (gmt 0)

10+ Year Member



Hi,

I wrote a little test script to display my problem (below).

This little script works fine on the command line, meaning you get the folloging :

Ok Parent
back to prompt
10 seconds later
Child (pops up)

which means, it works.

Now, When this script is exeuted by httpd... It prints Ok Parent (and sleeps 10 seconds).

"Child" does not print... it's normal

How do I get the CGI to fork properly under httpd?

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

print "Ok ";
if (fork()) {
# parent process
print "Parent ";
close(STDOUT);
exit;
}
# child process
sleep 10;
print "Child";
----------------------------------

SeanW

1:40 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



It probably is forking properly, it's just that since the process isn't attached to a tty, the child doesn't have a STDOUT file descriptor open.

In other words, a child process can't have any output when running as a CGI. If you need to communicate back to the parent, use some method of IPC (unix sockets, shmem, etc)

Sean

blakmonk

7:21 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



Hi SeanW

I fixed my problem by reopening STDOUT and STDIN on /dev/null.

apparently under httpd, I can't just close them, they need to be attached to something.

Thanx!

BlakMonk