Extracted at 2014-06-26.
pike.ida.liu.se
[Top]
Tools
Tools.Application
Tools.Application.Backgrounder

Class Tools.Application.Backgrounder

Description

Backgrounder is a convenience class that makes it straightforward to write applications that detatch from the console and enter the background (for example, daemon-like processes). The idea is that the main class which wishes to become a daemon should inherit this class, being sure that its create() function calls ::create() , see the note below and example.

Note

This class will use fork() on systems where it is available, however fork-like semantics should not be assumed, as other methods are employed when fork() is not available in order to achieve the goal of detaching from the controlling console.

Some systems, notably Windows, don't have fork() , so we must spawn a new pike process from the beginning. This class handles this madness automatically, though any class that inherits this must explicitly call create() with the command line arguments passed to it, so that it knows how to spawn the program correctly.

Programs using this class should be aware that on non-fork-enabled systems, additional command-line arguments will be passed to the new process so that the backgrounding state is available. Because of this, argument parsing code should be tolerant of extra arguments in the form of --tools-application-backgrounder*.

in this situation, this class will do some magic to make everything work, however the spawned background program will run everything up to the point that enter_background() , so everyhing before enter_background() be kept to an absolute minimum, such as limiting activities to parsing (but not acting on) command line arguments. There are likely other "gotchas" as well, so be careful out there!

Example

inherit Tools.Application.Backgrounder;

int should_background = 0; string logfile = "daemon.log";

protected void main(array(string) args) { ::create(args) }

void do_parse_args(array(string) args) { // let's pretend we've parsed arguments... should_background = 1; }

int main(int argc, array(string argv) { // first, we should parse any argumensts, and decide whether backgrounding is desired. do_parse_args(argc)

// second, if appropriate, enter the background. if(enter_background(should_background, logfile)) { return 0; } }