Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[RFC] A daemonizing module

30 views
Skip to first unread message

Giacomo Montagner (kromg)

unread,
Jan 10, 2013, 5:50:53 AM1/10/13
to

Hi,
I wrote a simple module to turn a Perl script into a daemon, in fact its name is

Script::Daemonizer

and I'm planning to upload it on CPAN. The aim of this module is to be simple:
while there are already fine modules like Proc::Daemon or Net::Daemon, I just
wanted a tool to turn an already existent program into a daemon with little
modification and little-to-no configuration. This module is able to:

- daemonize the process (go to background, close filehandles, etc...);
- write a pidfile and lock it (prevent double instances of the process);
- drop privileges;
- recirect STDOUT/STDERR to syslog.

Here's the rationale:

----------

NAME
Script::Daemonizer - Turns your script into a UNIX daemon process (the
easy way).


ABSTRACT
This module turns your script into a UNIX daemon by requiring as little
modification as possible, thus letting you concentrate on solving your problem,
rather than on writing a daemon. Just get your job done, then turn your script
into a daemon by calling daemonize().

It tries to redirect all messages to syslog by default, using
Tie::Syslog.

----------



I'm kindly asking for comments/suggestions/impressions about this idea (and, as
usual, about the namespace).


Thanks,
Giacomo

Stachu 'Dozzie' K.

unread,
Jan 10, 2013, 5:58:21 AM1/10/13
to
On 2013-01-10, Giacomo Montagner (kromg) <kromg...@gmail.com> wrote:
> Hi,
> I wrote a simple module to turn a Perl script into a daemon, in fact its name is
>
> Script::Daemonizer
>
> and I'm planning to upload it on CPAN. The aim of this module is to be simple:
> while there are already fine modules like Proc::Daemon or Net::Daemon, I just
> wanted a tool to turn an already existent program into a daemon with little
> modification and little-to-no configuration. This module is able to:
>
> - daemonize the process (go to background, close filehandles, etc...);
> - write a pidfile and lock it (prevent double instances of the process);
> - drop privileges;
> - recirect STDOUT/STDERR to syslog.

Have you compared your work with App::Daemon module? Though it redirects
STDOUT/STDERR to /dev/null instead of syslog (but on the other hand, it
pulls Log::Log4perl, and with Tie::Log4perl redirecting could be done
pretty easily).

--
Secunia non olet.
Stanislaw Klekot

Giacomo Montagner (kromg)

unread,
Jan 10, 2013, 3:10:50 PM1/10/13
to
On Thursday, January 10, 2013 11:58:21 AM UTC+1, Stachu 'Dozzie' K. wrote:

> Have you compared your work with App::Daemon module? Though it redirects
>
> STDOUT/STDERR to /dev/null instead of syslog (but on the other hand, it
>
> pulls Log::Log4perl, and with Tie::Log4perl redirecting could be done
>
> pretty easily).
>
>
>
> --
>
> Secunia non olet.
>
> Stanislaw Klekot

Ah, yes, that is one of the modules I tried. That is, in fact, the module that
made me think I wanted something easier, because I spent a whole morning
reading through its documentation and that of Log4Perl, and though it pushed
me to learn a whole bunch of new things, it's just what I wanted to avoid
(I mean, spending too much time learning something just to decide afterwards
it doesn't fit my needs).

I took inspiration from "regular" daemons (like named, (x)inetd, and so on),
and wanted something to turn my scripts into such a daemon quickly.
Besides, another feature one might take care of is reloading configuration
upon getting signaled with SIGHUP. That is something I have code for, but it's
not really something that fits into Script::Daemonizer (and it's simple/dumb
philosophy). I'm thinking to push this a bit further and use
Script::Daemonizer to build a UNIX::Daemon around it.
But that's a little more pretentious :$

Giacomo

parv

unread,
Jan 11, 2013, 1:41:03 AM1/11/13
to
in message <07de205b-9198-45ea...@googlegroups.com>,
wrote Giacomo Montagner (kromg) ...

> I took inspiration from "regular" daemons (like named, (x)inetd, and so on),
> and wanted something to turn my scripts into such a daemon
> quickly. Besides, another feature one might take care of is
> reloading configuration upon getting signaled with SIGHUP. That is
> something I have code for, but it's not really something that fits
> into Script::Daemonizer (and it's simple/dumb philosophy). I'm
> thinking to push this a bit further and use Script::Daemonizer to
> build a UNIX::Daemon around it. But that's a little more
> pretentious :$

Could it be possible to have a process re-exec after set period of
time (or everyday), with provisions for following either local time
zone or UTC? Something like ...

#!perl
...
use Daemonizer;
...
Daemonizer::setup( 'restart' => 'everyday' , 'time zone' => -1000 );
...


- parv

--

Giacomo Montagner (kromg)

unread,
Jan 11, 2013, 9:50:36 AM1/11/13
to
On Friday, January 11, 2013 7:41:03 AM UTC+1, parv wrote:

>
> Could it be possible to have a process re-exec after set period of
>
> time (or everyday), with provisions for following either local time
>
> zone or UTC? Something like ...
>
>
>
> #!perl
>
> ...
>
> use Daemonizer;
>
> ...
>
> Daemonizer::setup( 'restart' => 'everyday' , 'time zone' => -1000 );
>
> ...
>
>
>
>
>
> - parv
>
>
>
> --

It is possible, and it should not be complicated. Indeed the last feature I was
thinking to add before I publish Script::Daemonizer is a restart() subroutine
to ease the daemon self-restart (perlipc has the recipe in
http://perldoc.perl.org/perlipc.html#Handling-the-SIGHUP-Signal-in-Daemons
).

Doing this on a time-schedule basis is outside the scope of a daemonizing
module (expecially if you are trying to keep this minimalistic).
I see Schedule::Cron might provide the scheduling you're searching for.
Wrapping your own module around Script::Daemonizer and Schedule::Cron should
be easy, too.

I'm thinking about uploading sources on GitHub for a preview.

Giacomo


Giacomo Montagner (kromg)

unread,
Jan 11, 2013, 11:22:33 AM1/11/13
to
On Friday, January 11, 2013 3:50:36 PM UTC+1, Giacomo Montagner (kromg) wrote:

>
> I'm thinking about uploading sources on GitHub for a preview.

And so here it is:

git://github.com/kromg/Script-Daemonizer.git

0 new messages