tgummels has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have a daemon program that is using the signal handling
feature of the Event module to catch TERM and HUP. When
the daemon catches the HUP signal its supposed to unloop
re-read the config file and start the loop.

I'm looking for suggestions as to how I can return a status to
the caller so it knows the daemon actually did a refresh.  I was
thinking about named pipes or unix sockets or having the 
calling program tail a log file and grep for status.  
Any suggestions would be greatly appreciated.

Thanks
Travis

Replies are listed 'Best First'.
Re: How do I get status from daemon
by Zaxo (Archbishop) on Nov 06, 2001 at 19:33 UTC

    All the ways you mention are possible. The choice depends on how much other communication you need, and on the operating circumstances.

    A daemon usually sets up what its STDERR is to be (no controlling terminal), often a log file opened to append. You could write to STDERR on that assumption.

    Daemons often create a file named for their pid to aid in signaling. You could system {"/bin/touch /tmp/mydaemon/$$"}; whenever the config is read, then the curious can stat that for reassurance.

    A socket or pipe is fine if you have other uses for it, but is overkill for the message you want.

    After Compline,
    Zaxo

      { local *PID; open( PID, ">/tmp/mydaemon/$$" ) or warn "pid file: $!\n" }

      Or use utime if you want to diddle the modification time of an existing file rather than clobbering it. No need to start up an external touch (not to mention an external sh since you used one argument to system :).

Re: How do I get status from daemon
by traveler (Parson) on Nov 06, 2001 at 21:07 UTC
    It sounds like you might be on a UNIX or Linux system. If you are your system probably has syslog. You can send messages to syslog with the (standard) module Sys::Syslog. Your other program can watch for the log message to be sent to its destination.

    Depending on how portable you want to be and whether the HUPs are to be sent by humans or software you can also use semaphores. These are bits accessible to multiple programs that can be used to send one bit messages (e.g. "I processed the file"). See IPC::Semaphore. One item you need to manage is when to set the bit back to zero. The semaphore system has ways to deal with this (because semaphores are in groups), but the best approach to use depends on how your daemon and the sender of the HUP interact.

    It is interesting to note that while many programs do the log file or syslog thing, the sending programs often run "open loop" -- that is, they don't actually check that the update happened, they just assume it did. It is good to check it, though...

    HTH, --traveler