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.

whisper
  • 95
  • 1
  • 6
  • 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 : https://www.freedesktop.org/software/systemd/man/systemd.kill.html#KillMode= – Raxi Dec 31 '21 at 02:50
  • 1
    Daemons often create a so-called pid file in /var/run or /run typically named ".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. – Rachid K. Dec 31 '21 at 10:14

1 Answers1

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Thanks a lot. Besides these, is there any other potential risks need to care about if no clean up action in the daemon ? – whisper Dec 31 '21 at 06:25
  • 1
    @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. – Employed Russian Dec 31 '21 at 07:21
  • 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. – whisper Dec 31 '21 at 08:09