Creating a daemon with stop, start functionality in C Creating a daemon with stop, start functionality in C unix unix

Creating a daemon with stop, start functionality in C


  1. Write the pid of the daemon to /var/run/mydaemonname.pid so that you can easily look up the pid later.
  2. Set up a signal handler for SIGUSR1 and SIGUSR2.
  3. When you get SIGUSR1, toggle a stop flag.
  4. When you get SIGUSR2, set a report flag.
  5. In your while loop, check each flag.
  6. If the stop flag is set, stop until it is cleared.
  7. If the report flag it set, clear the flag and do your report.

There are some complications around stop/start, but if I'm understanding the question correctly, this should get you on the right track.

Edit: Added pid file as suggested by Dummy00001 in a comment below.


First, you probably don't have to do so much forking and house keeping yourself: http://linux.die.net/man/3/daemon

Next, remember that your daemon's interface to the world is probably through some sort of shell script that you also write located in /etc/init.d or whatever other distro-defined place.

So for the above answer, your shell script would send those signals to the pid of the process. There probably is a better way though. Signaling like above is a one way process, your controlling script has to jump through race-condition-prone and fragile hoops in order to confirm if the daemon successfully stopped or restarted. I would look for precedence and examples in /etc/init.d.