The original title was "apache2 and system", # 394424.
I'm trying to write a page that starts and stops a daemon. The symptom is that apache goes away and never comes back. Further actions on the page leave defunct processes in ps -ef (the called program or parent).
The original post was spot on for what I needed, so I tried it with different sleep times as the process to run.
The code is below. The amount of time ($tdiff) is always very low (about 0.004), however, the time apache "goes away" is directly proportional to the amount of time I set the sleep interval for (1, 10, 100).
It looks like perl finishes quickly, but apache does not return the page until all child processes finish, even if the perl process has.
Any advice on what to do to get the page back immediately?
#!/usr/bin/perl
use strict;
use Time::HiRes qw (usleep time gettimeofday);
use CGI qw/:standard *table start_ul use_named_parameters/; # load
+ standard CGI routines
use CGI::Carp qw /fatalsToBrowser/;
print "Content-Type:text/html\r\n\r\n";
print "<HTML>\n";
print "<HEAD></HEAD>\n";
print "<BODY>\n";
print "<CENTER>\n";
my $sleep = 100;
my $t1 = time;
print "using sleep of $sleep<BR>\n";
print "start time $t1<BR>\n";
# this was taken from the 2004 example vvvvvvvvvvvvvv
my $pid = fork;
die "Error forking: $!" unless defined $pid;
if (!$pid) {
my $pid = fork;
die "Error forking: $!" unless defined $pid;
if (!$pid) {
exec("sleep $sleep &"); # << except for this, changed from "f
+oo"
}
else {
CORE::exit;
}
}
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my $t2 = time;
my $tdiff = $t2 - $t1;
print "end time $t2<BR>\n";
print "difference = $tdiff<BR>\n";
print "</CENTER>\n";
print "</BODY>\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.