Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > forks from within a perl daemon process?

Reply
Thread Tools

forks from within a perl daemon process?

 
 
Dave
Guest
Posts: n/a
 
      06-19-2006
Hi,

I have a program that I have been running via cron that I thought might
be better run by running it as a daemon. When I created some test
daemon programs they all worked. When I placed my program inside of the
while loop, I noticed that the daemon killed its self somehow. I then
figured out that it died just after it tried launching a new fork
process -- and when I commented this out it worked fine.

I made some sample code based on how I am doing this. I have it set to
print output to the screen. I have it below to not use threads. That
will let you see what it is supposed to do. Then, if you uncomment the
'use forks' statement, comment the line beginning with &browserbuilder,
and uncomment the line starting with ${$i}=threads you will see what it
does in a threads-based setup.

Can anyone provide any insight in to why this is not working and
perhaps give some advice for what I need to do?

Thanks,
~dave

PS> I would like to use forks instead of threads although I have them
both in the program for testing.

----------------------------


#!/usr/bin/perl

use POSIX qw(setsid);

&daemonize;

#use forks;
#use threads;
#uncomment one of two above modules to use that package for threading
use strict;

while(1) {

my (@urls,@urls2,$ib,$i);
$ib="a";
print "Started at " . `date` . "\n\n";

@urls=qw/1 2 3 4 5 6 7 8 9 10/;

foreach my $xurlid(@urls)
{

$i=$ib . $xurlid; #makes each thread object unique by adding
it's url id to '$ib' defined above
print "about to fork $xurlid thread\n";
#${$i}=threads->create(\&browserbuilder, $xurlid, 'dave');
#uncomment above to use thread-based setup
&browserbuilder($xurlid,'dave');
#uncomment above to use non-thread setup
push @urls2, $i; #used for checking thread status later
select(undef, undef, undef, 0.07); #sleep for 70 milliseconds
}

###########
# Go through each thread and wait for it to close so the program
doesn't exit early
#foreach my $url(@urls2)
# {
# @{$url} = ${$url}->join();
# print "$url returned: ${$url}[0]\n";
# }
###########
print "Ended at " . `date` . "\n\n";
sleep 10;
}
#end loop

sub browserbuilder
{

my $num=shift @_;
my $name=shift @_;

print "\n$name forked $num correctly\n\n";
return 0;

}

sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
# open STDOUT, '>>/var/log/uu_info.log' or die "Can't write to
/dev/null: $!";
open STDERR, '>>/var/log/uu_errors.log' or die "Can't write to
/dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
umask 0;
}

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-19-2006
"Dave" <(E-Mail Removed)> wrote:
> Hi,
>
> I have a program that I have been running via cron that I thought might
> be better run by running it as a daemon. When I created some test
> daemon programs they all worked. When I placed my program inside of the
> while loop, I noticed that the daemon killed its self somehow. I then
> figured out that it died just after it tried launching a new fork
> process -- and when I commented this out it worked fine.


Presumably it died with a message. What was that message?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-19-2006
"Dave" <(E-Mail Removed)> wrote:
> Hi,
>
> I have a program that I have been running via cron that I thought might
> be better run by running it as a daemon. When I created some test
> daemon programs they all worked. When I placed my program inside of the
> while loop, I noticed that the daemon killed its self somehow. I then
> figured out that it died just after it tried launching a new fork
> process -- and when I commented this out it worked fine.
>

....
>
> Can anyone provide any insight in to why this is not working and
> perhaps give some advice for what I need to do?



> #!/usr/bin/perl
>
> use POSIX qw(setsid);
>
> &daemonize;
>
> #use forks;


You can't call fork manually (which daemonize does) after "use forks;" is
invoked (which happens at compile time), or things go wonky.

so you can either change "use forks;" to:
eval "use forks";

Or you can take the contents of "sub daemonize {...}" and move them into
a BEGIN block which precedes the "use forks" statement.

There seemed to be some other problems with your test code, so I can't
guarantee that this is your only problem....

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-20-2006
Dave <(E-Mail Removed)> wrote:


> I have a program that I have been running via cron that I thought might
> be better run by running it as a daemon.



What did you observe about its behavior that made you think that?

If it isn't broken, then don't fix it.

If it is broken, then tell what is broken about it.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      06-20-2006
Hi,

Thank you very much Xho -- at least at a cursory glance, that seemed to
do the trick. I will test with it more today. I never would have
thought of that -- thanks again. I apologize for not replying to the
'what was the exit message' as I only just logged back on to the
newsgroups. It did not exit with any message that I saw, it just went
away.

Tad - It was working ok with cron, but when I fork ~ 2500 routines at
once or so every 5 mins, the load goes up to 6-10 so I thought better
if I spaced those forks out over every 15 seconds or so. I don't know
if that is better yet as I haven't tested it, but I think it will
probably reduce the strain quite a bit.

Thanks very much. Other peoples' knowledge never ceases to amaze me
~dave

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: PEP 3143: Standard daemon process library (was: Writing awell-behaved daemon) Floris Bruynooghe Python 1 03-24-2009 02:58 PM
Re: PEP 3143: Standard daemon process library (was: Writing awell-behaved daemon) Jean-Paul Calderone Python 0 03-20-2009 01:02 PM
Daemon Win32::Daemon; ph1975@gmail.com Perl Misc 0 09-07-2006 10:58 AM
capturing perl forks patrisha@alumni.washington.edu Perl Misc 5 02-03-2005 02:13 AM
Recording Process Startup (forks/exec etc) in Unix The Boss C Programming 1 10-28-2003 02:30 AM



Advertisments