You might just run a script like this as your daemon:
false #<-- replace with daemon stuff (perhaps exec daemon $*)
while ( daemon_loop $* ); do
This will restart the daemon when it exits unless it exits with an error. You'd need to make the actual daemon exit without error when given e.g. the SIGHUP
signal to indicate that it should be restarted.
PS Another option is to design it to work with xinetd
, which won't actually start it until a connection is made; there will be one fork per connection.