I'm looking for a standard way or best practice to keep a daemon started by an init.d
shell script alive.
Or even better, is there a way to keep it alive directly from /etc/init.d
?
Specifically, I have a daemon called dtnd
with and infinite loop that looks for down precess, if there are any, the daemon wake up them again. Also, I use the start-stop-daemon
tool in order to let the precess by run from a given system user.
I want to run this dtnd
daemon from startup. In order to achieve this behavior I created a init.d script that "wraps" the dtnd
file using start, stop and status commands.
I have 2 questions that I will like to solve:
Is there a way to achieve keeping alive some process from init.d shell script. Is an standard/best way practice?
It's recommended to keep a daemon alive with infinite loop? I guess it's better to use some tool like daemon to achieve that. It's correct?
Thank you so much for your time!
You might want to use the daemon(3) library function inside the code of your daemon. You should be aware of syslog(3) (at least to log error conditions). You probably should catch gently the SIGTERM
signal. Read carefully signal(7)
Server programs are often event loop based (and it is conceptually an infinite loop). You'll either use en existing event loop library (e.g. libev, libevent, glib, ...) or build your own one around a multiplexing syscall like poll(2)
Read Advanced Linux Programming and study the source code of some existing free software daemon.
Perhaps dbus is also relevant for your goals (which I don't really understand: what does "looks for down process" exactly means? You could set some limits with setrlimit(2) often invoked thru the ulimit
bash builtin in some .bashrc
)
There is also the @reboot
entry for crontab(5), but that is not the recommended practice for system daemons (however you could use it in your user crontab file).
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