It seems that a common way of running only one copy of a process is to write a pid to a file and then on start check whether a process with that pid exists. I imagine OS is trying not to reuse a pid quickly after a process has crashed, but since the number of pids is limited, sooner or later there is going to be another unrelated process using that pid. And the original one won't start. How can this situation be avoided?
Asked
Active
Viewed 202 times
0
-
Can you elaborate "running only one copy of a process is to write a pid" ? How is it related to PIDs? – P.P Jun 30 '14 at 17:41
-
This is probably not related to StackOverflow, looks more like an OS question. – Alexandre Lavoie Jun 30 '14 at 17:42
-
I think this belongs on unix.SE. – djechlin Jun 30 '14 at 17:43
-
Ok, how do I move it? – Bob Jun 30 '14 at 17:45
-
If the PID file is deleted when the process exits, it works well. The PID can be used to kill the running process when needed. – Danke Xie Jun 30 '14 at 17:46
1 Answers
1
Typically you would also check for the process name. For example the following will return 1 if there is a process called fork-server running on PID 10616:
ps -xp 10616 | grep fork-server | wc -l
It is of course still possible to have a collision, but the likelihood should be much less.

GeekyDeaks
- 650
- 6
- 12