0

Possible Duplicate:
How can I run a Perl script as a system daemon in linux?

A number of maintenance shell scripts I'm writing need to be daemonized, and I'd like a library that does something like the following:

script

#!/usr/bin/...

use daemonize;

...

Or

daemonize('/path/to/external/script/or/program');

And then:

./script start
./script status
./script stop

This has to run as non-root, and not part of the init process.

I'd prefer a library in Perl or perhaps Python.

I've considered libslack.org's "daemon", but that would require writing my scripts in C and compiling them. I'd also still have to write code to handle command line arguments. The other option was to call a compiled version of "daemon", but that seemed messy. Like I said, a simple Perl or Python solution would be preferred.

Community
  • 1
  • 1
Clinton
  • 22,361
  • 15
  • 67
  • 163

3 Answers3

2

Have you investigated Proc::Daemon.

Proc::Daemon - Run Perl program(s) as a daemon process.

Alternatively there is Working::Daemon

Working::Daemon - Perl extension for turning your script inta daemon.
It also supports start,stop,status and restart.

Also discussed at How can I run a Perl script as a system daemon in linux?

Other references : http://www.perlmonks.org/?node_id=478839 http://erwan.lemonnier.se/talks/working_daemon.pdf

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
2

Karthik T gave the answer for perl. Or, rather, some of the best answers, because of course there are many ways to do it. For Python, of course, there should be only one way to do it.

Fortunately, PEP 3143 specifies what a Python daemon library should do, and there's a reference implementation at python-daemon, which will hopefully be in the standard library in some future 3.x version.

If for some reason this doesn't work for you (e.g., IIRC, it requires 2.6+ or 3.1+, and you may have 2.5 or 3.0), the PEP has links to various other modules and recipes at the end.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Previous post on this topic for python - [How do you create a daemon in Python?](http://stackoverflow.com/q/473620) – Karthik T Jan 08 '13 at 01:36
  • @KarthikT: That question is out-of-date… but fortunately, Jeff Bauer added a comment to his own answer updating it to say basically the same thing I said above. – abarnert Jan 08 '13 at 02:28
  • 1
    ah yes I noticed that, but the second answer does mention the same PEP that you are talking about right? – Karthik T Jan 08 '13 at 02:48
  • @KarthikT: Honestly, as soon as I saw that the author of the first answer had added that info in a comment to his answer, that was good enough… but you're, the next answer also mentions and links to the PEP and `python-daemon`, and the one after that is all about `python-daemon`… So yeah, the information is definitely there, as long as you get to any of the comments on the accepted answer, or any of the other answers, or really anything beyond the first link. – abarnert Jan 08 '13 at 10:12
1

This isn't an exact fit in terms of the way that you interface with the library, but supervisord is a system that's widely used in web operations. Rather than including a reference to it in your script, you write a configuration for each program you want to daemonize (call one server), in which you specify the script to run, various environment variables, and even the number of instances of the program to run. Then you issues commands to it as follows:

$ supervisorctl start server
$ supervisorctl stop server

You can also tell supervisord to start or stop all processes that it manages:

$ supervisorctl start all
$ supervisorctl stop all

This approach is great since it doesn't require you to modify your own code. As a result, it's agnostic to the language that your program is written in, which is another perk.

seliopou
  • 2,896
  • 20
  • 19