Daemonize perl script

I am currently looking for a daemonize perl script. Unfortunately, most of the answers are outdated, and I really don’t really understand how to start the daemon process (especially perem daemon scripts).

Now I'm looking at Proc Daemon, but again I don’t know where to start, or whether it should be done with or without modules.

I believe that if I give an example of what I am looking to ask this question a little more.

Example

Say I'm on osx and I want to write a perl script that can work as a daemon. It responds to a HUP signal, which then proceeds to print the contents of the file from a specific directory. If it receives a USR1 signal, it prints the content in different ways. What is the most suitable way to do this as a demon?

+4
source share
1 answer

This is all you need:

#!/usr/bin/perl

use strict;
use warnings;

use Daemon::Daemonize qw( daemonize write_pidfile );

sub sighup_handler {
   ...
}

sub sigusr1_handler {
   ...
}

{
   my $name          = "...";
   my $error_log_qfn = "/var/log/$name.log";
   my $pid_file_qfn  = "/var/run/$name.pid";

   daemonize(
      close  => 'std',
      stderr => $error_log_qfn,
   );

   $SIG{HUP}  = \&sighup_handler;
   $SIG{USR1} = \&sigusr1_handler;

   write_pidfile($pid_file_qfn);

   sleep while 1;
}
+8
source

Source: https://habr.com/ru/post/1584822/


All Articles