If you fork() without ever waiting on your children, you will accumulate zombies:
$SIG{CHLD} = sub { wait };
There's also the double-fork trick (error checking on fork() returns omitted);
unless ($pid = fork) {
unless (fork) {
exec "what you really wanna do";
die "no exec";
# ... or ...
## (some_perl_code_here)
exit 0;
}
exit 0;
}
waitpid($pid,0);
######################################################################
# Necessary Variables: #
# The following variables should be set to define the locations #
# and URLs of various files, as explained in the documentation. #
#---Modification by Ron Perry aka ronzta
use CGI;
$cgi = new CGI;
my $RandomTextFile = $cgi->param('file');
#$RandomTextFile = "randomtext.txt";
# En: Path of Text file.
# Fr: Chemin du fichier de textes.
#----end of modified code------
$Delimiter = "\n--NEXT--\n*"; # En: Tags for Next Texte.
# Fr: Separateur de texte.
# Options:
$UseLog = 0; # Use Log File? 1 = YES; 0 = NO
$LogFile = "/Absolute/path/to/Random-Text-Log.txt";
# Nothing Below this line needs to be altered! #
######################################################################
srand(time);
open(LINKS,"$RandomTextFile") ¦¦ &Error("Cannot Open Links File : $RandomTextFile, Error $!\n");
@TextFile = <LINKS>;
close(LINKS);
$NbLines = @Text = split(/$Delimiter/, join('',@TextFile));
$Phrase = $Text[int rand $NbLines];
if ($UseLog) {
@date = localtime(time); $date[4]++; $date[5] += 1900;
$Time = "$date[4]/$date[3]/$date[5]";
open(LOG,">>$LogFile") ¦¦ &Error("Cannot Write Log File : $LogFile, Error $!\n");
print LOG "[$Time] - $ENV{'REMOTE_HOST'} -> $Url\n";
close(LOG);
}
print "Content-type: text/html\n\n";
print $Phrase."\n";
exit(0);
sub Error {
my($ErrorText) = @_;
print "Content-type: text/html\n\n";
print "Error: ".$ErrorText;
exit(0);
}
[edited by: jatar_k at 8:27 am (utc) on Feb. 11, 2005]
[edit reason] removed urls [/edit]
Also srand doesn't really need a seed, but it shouldn't cause that either . . .
This works:
<!--#exec cgi="/cgi-bin/randomx.cgi" -->
This doesn't:
<!--#exec cgi="/cgi-bin/randomx.cgi?file=abc.txt" -->
To use multiple input text files you'll need a script for each, hardcoded with the appropriate file name.
.