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

I need to write an application that forks and runs as a daemon that will only have one instance in the air at a time.
$ daemon start Running... $ daemon start Is already running...
I also have to provide a way to stop the daemon
$ daemon stop Exiting ...
It would be also nice if I could also ask the status that could say if the daemon is running or not. E.g.
$ daemon staus Daemon is running
The daemon is currently creating a file $HOME/.daemon with it PID as the content and when I run daemon stop the new process sends KILL to the PID it finds in the file. But what if the original daemon crashed and not there is a totally unrelated process with that PID? Any code examples ?

Replies are listed 'Best First'.
Re: One process at a time
by merlyn (Sage) on Aug 25, 2005 at 15:36 UTC
Re: One process at a time
by fmerges (Chaplain) on Aug 25, 2005 at 16:21 UTC

    Hi,

    Normally you do the stop/start/restart etc things using a external sh script that do the job...

    And in your daemon you check on startup if another instance is still running, if so, then exit

    Once your program/daemons ends, because, normally a signal it receives (15, 9), the program/daemon should remove the PID file

    You can say, ok, if the daemon exits because any fault, I don't let him start again, because there is a untreated error... that's made automatically if you only check the PID file, and not the second one, to see if this PID number is valid...

    If you want that this program/daemon is running forever, you can include it in the init or check it with any tool, like 'supervise', from D.J.Bernstein, see daemontools...

    Don't try the complicated approach, the status report can be get from a log/status file that your daemon write. For example like AMANDA does.

    Read some text about daemons and UNIX... there you'll get all the things well explained.

    And remember, K.I.S.S. (Keep It Simple Stupid)

    Regards,

    |fire| at irc.freenode.net
Re: One process at a time
by sgifford (Prior) on Aug 25, 2005 at 16:25 UTC
    I use daemontools to solve this problem. It's not itself a perl program, but it is perfectly happy to supervise one; I use it that way all the time. I like that I can run most of my servers under it, and manage them all exactly the same way, including their logging.
Re: One process at a time
by philcrow (Priest) on Aug 25, 2005 at 16:09 UTC
    Follow the above FLOCK advice of merlyn for how to keep two instances from running at once.

    If you're on Linux, consider putting a script to start/kill your daemon in /etc/rc.d/init.d.

    Then you can say

    service your_service_name_here start service your_service_name_here stop

    Follow the pattern of the other items in /etc/rc.d/init.d and use chkconfig to install your script into the proper run levels. Then it will start when the system boots and stop when it shutsdown.

    To make your script a daemon in the first place you might try Proc::Daemon.

    Phil