2

In linux, is there a way to make a daemon "unique" ?

Ie if the daemon is already started/running, not start it again. I think it can be done by creating a specific file when the daemon is first launched, then checking this file, but this creates issues if the daemon is killed and the file is not erased. Is there a more 'linux' way to check if a given daemon (whose code I control) exists ?

1
-1

You've essentially described PID (Process ID) tracking, which is commonly done by a service manager, such as systemd, or by script written for the specific daemon. Upon launching the process, the PID is written to file, this is later used to check the status of the process or stop the process. This does not prevent another copy of the program running, just another instance being started by the service manager or startup script.

Dealing with a "stale pid file", where the process is not running but the file still exists, is detected by listing processes with that ID. If no matching process if found, it can be assumed to be improperly stopped and the PID file deleted.

4
  • Actually, it is uncommon for service management, and this description is from the 1980s. Even then, we knew that PID files were rickety and dangerous. The right answer is to simply remember the forked process ID, and not fork more than one child at a time, as proper service managers actually do.
    – JdeBP
    May 4 '20 at 23:33
  • There are currently supported Linux distributions that use Sysvinit (Unix System 5) style scripts that track PID files this way. Your suggestion is certainly A way to accomplish this and is a more modern way of doing it; however, not the only way. I was also intentionally trying to keep it simple as the OP seems inexperienced.
    – virullius
    May 5 '20 at 2:55
  • You are repeating outright bad advice, that the world has known to be bad for over 30 years, and the fact that van Smoorenburg rc can do PID files is no excuse. The questioner explicitly asked for a "more Linux way" of doing things, and did not restrict this to van Smoorenburg rc. There have been toolsets for dæmon management that do this the right way since the 1990s, easily deployable with van Smoorenburg rc. System 5 Unix gained a service manager in 1988.
    – JdeBP
    May 5 '20 at 6:49
  • I think you're being overly dramatic and opinionated and the community would be better served if you were to give your own answer explaining the more modern way of doing this. I intentionally gave a simplistic answer to a simplistic question.
    – virullius
    May 7 '20 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.