Results 1 to 5 of 5

Thread: Creating a daemon using Perl

  1. #1
    Join Date
    Jan 2010
    Posts
    49

    Creating a daemon using Perl

    I try to create a demon with proc:: daemon, but for now, I can not complete it. My script is launched, but I wanted to create a pid file containing the number of processes assigned by the system. Can you give an example to get to do that?

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    re: Creating a daemon using Perl

    There's certainly better but:

    You can be "demonizing" your hand process, in which case you can do whatever you want with the pid by setsid (), starting from this example from perlipc :
    Code:
    use POSIX 'setsid';
     
    sub daemonize {
      chdir '/' or die "Can't chdir to /: $!";
      open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
      open STDOUT, '>/dev/null'
      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: $!";
      open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
    }

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    re: Creating a daemon using Perl

    I do not find this solution robust. I think you are on Linux/Unix, then Proc:: Daemon is for the daemon and it works. It handles everything correctly.

    Then, if you want to retrieve its pid, I recommend the Proc:: ProcessTableYou module that can read all the running processes on your server and using a regex, you can get your script running in daemon.

  4. #4
    Join Date
    Jan 2010
    Posts
    49

    re: Creating a daemon using Perl

    Apparently it works this way:
    Code:
    # daemonize
    my $pid_file = '/var/run/daemon.pid';
    Proc::Daemon::Init;
    open(PID,$pid_file);
    my $pid = <PID>;
    close PID;
    if ($pid and kill 0 => $pid) { &set_log($log,"Pid present and reactive","info");  die;}
    else {
            open(PID,"> $pid_file");
            print PID $$;
            close PID;
    }
    The problem is that when I run my script, I send a log to a file that is found in the pid file also (basically I have the pid monitor my log):
    Code:
    # declaration of the connection
    my $server = IO::Socket::INET->new(
            Proto => 'tcp',
            LocalAddr => $serv_ip,
            LocalPort => $port,
            Listen => SOMAXCONN,
            Reuse => 1
    );
     
    die "Unable to start the server" unless $server;
    &set_log($log,"Server $serv_name is ready to receive clients","info");
    the set_log uses Log:: Handler:
    Code:
    sub set_log {
            my ($log,$logmsg,$level) = @_;
            $log->$level("$logmsg");
    }
    and the result in the pid file:
    Code:
    21233aoû 07 13:33:10 [INFO] Test Server is ready to receive clients
    I'll try with setsid to see what happens, but it is a pity as Proc:Daemon done it all by himself.

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    re: Creating a daemon using Perl

    By hand, I'd like that (I'm not sure I was clear earlier).
    Code:
    use POSIX qw(setsid);
     
    chdir '/'                 or die "Can't chdir to /: $!";
    umask 0;
    open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
    open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
    open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
    defined(my $pid = fork)   or die "Can't fork: $!";
    exit if $pid;
    $pid = setsid          or die "Can't start a new session: $!";
    open my $fh, '>>', 'path/to/logfile' or die "Can't open log file:$!";
    print $fh "$pid\n";
    close $fh;
     
    while(1) {
       # ...
    }
    In fact this is exactly what Proc:Daemon
    Code:
    perldoc -q daemon
     
           How do I fork a daemon process?
     
           If by daemon process you mean one that’s detached (disassociated from
           its tty), then the following process is reported to work on most
           Unixish systems. Non-Unix users should check their Your_OS::Process
           module for other solutions.
     
           ·   Open /dev/tty and use the TIOCNOTTY ioctl on it. See tty for
               details. Or better yet, you can just use the POSIX::setsid()
               function, so you don’t have to worry about process groups.
     
           ·   Change directory to /
     
           ·   Reopen STDIN, STDOUT, and STDERR so they’re not connected to the
               old tty.
     
           ·   Background yourself like this:
     
                   fork && exit;
     
           The Proc::Daemon module, available from CPAN, provides a function to
           perform these actions for you.

Similar Threads

  1. How to do log analysis in Perl?
    By Atsushi in forum Software Development
    Replies: 5
    Last Post: 24-01-2011, 11:29 AM
  2. What is PERL and what are its uses?
    By Gadin in forum Software Development
    Replies: 10
    Last Post: 08-01-2011, 07:33 PM
  3. Creating graphical ftp client with Perl using class NET:: FTP
    By Udayachal in forum Software Development
    Replies: 6
    Last Post: 16-02-2010, 01:43 AM
  4. What are the different version of perl
    By Toraidio in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 02:47 PM
  5. What is hal daemon?
    By konqueror101 in forum Operating Systems
    Replies: 3
    Last Post: 06-08-2009, 10:40 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,257,886.66117 seconds with 17 queries