1

Commonly a long running program will have it's pid saved in a file. This is not reliable as the pid will be reused by other programs if the original program exits. I think also the program can modify its /proc/cmdline and other pgreppable features.

What is a reliable way to store a handle to the program to facilitate later KILL or other interaction?

I think if the parent of the original program stays alive, it can rely on the pid being correct, because the pid will not be re-used until the parent wait(2)s. Perhaps this could be used by having a wrapper parent that only starts the subprocess and manages the child pid file.

Is there a robust implementation of this parent solution?

Is there a solution without relying on the parent staying alive?

Evan Benn
  • 1,571
  • 2
  • 14
  • 20
  • See [What's the best way to make sure only one instance of a Perl program is running?](https://stackoverflow.com/q/455911/6862601). You could use a PID file that is unique to the program/process and make the program lock it. The lock would go away when the process exits – codeforester Feb 22 '19 at 04:32
  • Another similar post: [Quick-and-dirty way to ensure only one instance of a shell script is running at a time](https://stackoverflow.com/q/185451/6862601). – codeforester Feb 22 '19 at 04:36
  • thanks for the help, using a lock on the pidfile helps ensure only 1 process of some class is running, but does not answer my question. The contents of the pidfile is still an unreliable 'handle' for the process. you cannot safely kill whatever pid you find in your pidfile. – Evan Benn Feb 22 '19 at 04:51
  • Basically, you try to lock that PID file and if it is already locked, it means the process is still active. – codeforester Feb 22 '19 at 06:07
  • Thanks codeforester, that makes 1 step of more sense, but there is still an obvious race condition in your sentence. LOCK the pid file and if it is LOCKed then the pid in the file is guaranteed to be the pid of the program. Except it could be because another 'tester' program just tried to lock the pid file, or in the time between trylock and reading the pid, the pid has been reused. I am looking for something reliable! – Evan Benn Feb 25 '19 at 05:13

0 Answers0