Hello fellow monks,
I created a simple 'server' script that will accept input from a cgi script. The server will handle multiple connections on the same port, also. In a nut shell, this 'server' script will take in variables, and fork off a process to sleep for a given amount of time, and then send the user an email (and perform other functions).
Here is my problem: I receive a confirmation e-mail once the connection has been made, but i do not receive the other e-mail (which is supposed to be sent from the forked process once the sleep() is up). I receive one confirmation e-mail per client, but I will never receive the second e-mail. I don't know why the forked process is not working as I think it should?
Also, the problem ONLY happens when I daemonize the process. If i simply run the script inside a terminal 'perl server.pl', it will work perfectly fine and I will get all the desired results. However, when I daemonize it by 'perl server.pl &', the functions within the fork, including the second e-mail, will not be executed.
Shouldn't I be able to daemonize it and get the same results?
Here is the code:
#!/usr/bin/perl
# includes and dependencies....
use strict;
use warnings;
my $max_clients = 10;
my $port = 15100;
while(1)
{
my $sock = new IO::Socket::INET (
LocalHost => 'localhost',
LocalPort => $port,
Proto => 'tcp',
Listen => $max_clients,
Reuse => 1,
);
die "Could not create socket: $!\n" unless $sock;
my $new_sock = $sock->accept();
while(<$new_sock>)
{
# parse information
close($sock);
#send e-mail function here
unless(fork)
{
# sleep for given time
sleep(30);
# perform function
#2nd send e-mail function here
exit(0);
}
}
}
Thanks in advance!
In reply to Fork issues
by 3SRT
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.