These questions are quite general since they keep coming up for me in different situations. I'm hoping there are some basic principles/standard practices.
The typical requirements:
My questions:
Thanks
- Should I write the program as a "daemon"?
No.
Do not try to daemonize yourself. Use OS provisioned tools to make your application run in background from a startup script, like start-stop-daemon
in Debian/Ubuntu. Systemd and upstart can also handle this for you in their startup scripts, as can most init systems these days.
Writing a daemon has some pitfalls you might not expect, and most modern init scripts don't expect you to do send your own process to background - which would complicate their jobs anyway. This allows for example generating reliable .pid
files for tracking your application's process id. If you daemonize yourself, your init system has to rely on your application to correctly communicate your process id somehow since you generate new PID's the init system can't track. This complicates things both for them as for you.
I know you are thinking c/c++ when asking this question but I think it's more general than that and the logic used when designing is independent of the language used for the implementation.
There is a python enhancement proposal (PEP 3143) that was used to describe the standard daemon process library that has now become part of the language. If you look in this section on correct daemon behavior it describes how a daemon should act. There are also considerations of the differences between a 'service' and a daemon.
I think that should pretty well answer your general questions on daemons and their behavior. Check out W. Richard Stevens' Home Page and you can find info on 'Unix Network Programming', Prentice Hall which has more information specific to c/c++ when coding daemons in a *nix environment and best practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With