Re: Writing a daemon that can be used with service ?




dom wrote:
I'm confused...

This web site tell you how to write a daemon:
http://www.linuxprofilm.com/articles/linux-daemon-howto.html#s1

This looks like it produces a straight executable with an infinite while
loop. What it doesn't say is how to stop the daemon gracefully.

The web page you pointed to /does/ cover "how to write a daemon", in
that everything up to the "while (1)" loop is what you need to do to
make a daemon.

However, the page misleads by giving you the "while(1)" loop as the
active body of the daemon. Substitute what ever logic you want the
daemon to do for that loop and you'll be OK.

Most probably, you'll want the daemon to do something repeatedly. For
that, you /would/ code a loop. However, you would code the loop so that
it contains a reasonable exit or termination condition and process.
This /may/ be a
while (test_for_exit_condition())
loop
or an infinite loop, as you prefer. If you use an infinite loop, you
are mostly restricted to implementing your termination and exit logic
within a signal handler or other asynchronously invoked callback.

You /can/ use signals and a signal handler to set the value that the
test_for_exit_condition() test looks at, or you can use some other
input instead. You may choose to permit the client to issue a shutdown
command through whatever internal channel the daemon opens normally,
and take that value as the true value for test_for_exit_condition().
OTOH, you may look for the presence or absence of a sentinal file in
the test_for_exit_condition() logic, and return true or false depending
on that external condition. In any event, the termination process, like
the "work" of the daemon, is up to you

HTH
--
Lew

.



Relevant Pages

  • Re: newbie: need inspiration
    ... Such daemon should have some exit function. ... > Overloading a variable is when you give it more than one purpose. ... the variable value holds the users input while the variable ... Loop should end when user ...
    (comp.unix.shell)
  • Re: will sleep() *terminated* the program?
    ... > will the double while loop helps instead of the single while loop? ... I know this is odd indeed. ... > what about make the proxy program into a daemon process, ... Do you start your program in background and then exit the shell? ...
    (comp.unix.programmer)
  • pause() and raise()
    ... signal handlers ), enters a forloop and calls ... to the daemon to invoke code triggered by the reception of these 2 ... SIGUSR1 and SIGUSR2 to itself at a user defined interval, ... call raisefrom within the forloop containing the pause ...
    (comp.unix.programmer)
  • Re: Writing a daemon that can be used with service ?
    ... This looks like it produces a straight executable with an infinite while loop. ... What it doesn't say is how to stop the daemon gracefully. ... - the start/stop rc script, which is used to start/stop/check the daemon process. ...
    (comp.os.linux.development.apps)
  • Daemon-writing
    ... I'm currently engaged in writing my first daemon, ... URL A doesn't recommend handling any signals. ... loop that follows the call to daemonize. ...
    (comp.os.linux.development.apps)