Skip to contentSkip to conversation

daemons

DISCUSSION ameer armaly 3 participants 3 messages Feb 15, 2004

lore
Share & tools
lore
#0 lore raw

Hi all.
What do I need to do to have a program invoke as a daemon; i.e sit and ait for a particular event to happen, and then do something, all in the background?
Thanks,

Ameer

#1 lore raw
Quoted 3 lines · ameer armaly

What do I need to do to have a program invoke as a daemon; i.e sit and ait for a particular event to happen, and then do something, all in the background?

The topic of daemon processes gets its own chapter in Stevens[1], but the short version is:

1. Call fork(), and have the parent exit.

2. Call setsid() to create a new session and disconnect from the controlling terminal.

3. Call chdir() to change the current directory to the daemon's working directory, or the root directory if it doesn't have one.

4. Call umask(0) to reset the file permissions mask.

5. Close all unused file descriptors.

[1] Advanced Programming in the Unix(R) Environment W Richard Stevens
Addison-Wesley

Depending upon what the daemon does, other useful steps may include:

6. Reset the environment to a fixed set of values.

7. Set up descriptors 0, 1 and 2 (use /dev/null if nothing else is appropriate).

8. Reset other aspects of the process' environment which will have been inherited, e.g. resource limits.

Signature · 1 line
Glynn Clements <[email protected]>