2

I'm try to create a daemon application by following the example :

Creating a daemon in Linux

In the example, there is no operations about the daemon stop.

Therefore, I'm curious about :

  1. How to send stop signal to the daemon ? Or just kill PID directly ?
  2. If the daemon only can stop on killed, should the daemon itself do the cleaning up operations on killed ? Just like close file descriptors, saving parameters, etc.

Thanks in advance.

2
  • 2
    Nowadays, in the systemd world, i think you just begin graceful shutdown when you receive the SIGTERM signal (which can be send by kill indeed). This information might be of use too : freedesktop.org/software/systemd/man/… Commented Dec 31, 2021 at 2:50
  • 1
    Daemons often create a so-called pid file in /var/run or /run typically named "<daemon-name>.pid" into which their pid is stored. This can be used to retrieve the pid of the daemon from its name and send a SIGTERM signal to make it finish gracefully. Commented Dec 31, 2021 at 10:14

1 Answer 1

2

should the daemon itself do the cleaning up operations on killed ? Just like close file descriptors, saving parameters, etc.

Closing file descriptors is pointless -- they'll be automatically closed by the kernel when the process exits.

For "saving parameters", it depends on what you mean by "parameters". If you mean the command-line arguments, then no: when the daemon is restarted, it will get a fresh copy of these.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. Besides these, is there any other potential risks need to care about if no clean up action in the daemon ?
@whisper There are quite a few things you may need to worry about, depending on what exactly your daemon is doing. SysV shared memory for example doesn't get cleaned up (which is also why you should avoid using it). Without knowing anything about your daemon, it's hard to guess what (if anything) you may need to clean up.
thanks, it's enough, I just start up test the daemon and do not have clear the idea what kind of resource may be token. I will keep it in mind with the program development going.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.