problem with using the forks module within a daemon setting

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default problem with using the forks module within a daemon setting

    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;
    }

    Dave Guest

  2. Similar Questions and Discussions

    1. Win32::Daemon - multiple processor problem
      All, I have recently developed a Win32 service (using Win32::Daemon) on my single processor box, then deployed it onto a multi-processor box (P4...
    2. Mac projector/resource forks problem?
      Hi Is there a way to prevent Director projectors and xtras losing resource forks when copied using Java More specifically I'm using...
    3. Daemon proc run on a port, kill daemon and cannot restart on same port for 10 minutes.
      Unix is AIX 4.3 The daemon is a license manager which runs for an application. Killing the daemon is fine. (using the 'kill -9 <pid>')...
    4. [PHP-DEV] Setting PHP module values in C
      I'm writing an Apache module and I'd like to be able to dynamically change some of PHP's settings from within this module. Mainly I need to be...
    5. Problem with Linux client daemon connecting on bootup
      Will Hatcher wrote: Do you have $ORACLE_HOME, $ORACLE_SID and $LD_LIBRARY_PATH set? What about $TNS_ADMIN (if your TNS files are not located...
  3. #2

    Default Re: problem with using the forks module within a daemon setting

    "Dave" <dszostek@gmail.com> 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.
    Please don't multipost. See my response in comp.lang.perl.misc

    Xho

    --
    -------------------- [url]http://NewsReader.Com/[/url] --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    xhoster@gmail.com Guest

  4. #3

    Default Re: problem with using the forks module within a daemon setting

    Sorry Xho -- after I posted it here I thought it would be better in the
    ..misc group. I couldn't find how to delete it. I'll post there first
    next time. Thanks for your help.

    [email]xhoster@gmail.com[/email] wrote:
    > "Dave" <dszostek@gmail.com> 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.
    >
    > Please don't multipost. See my response in comp.lang.perl.misc
    >
    > Xho
    >
    > --
    > -------------------- [url]http://NewsReader.Com/[/url] --------------------
    > Usenet Newsgroup Service $9.95/Month 30GB
    Dave Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139