0

My title is more than no explicite so feel free to change it (don't really know how to name it)

I use a php script to check if a list of pid is running, My issue is that pid identifying is not enough and some other program can get the pid number later on when mine is over.

So, is there something I can do to identify than pid is the good pid that I need to check and not another one.

I think to hash /proc/<pid>/cmdline but even that is not 100% safe (another program can be the same software and the same parameters (it's rare but possible).

if an example is needed:

I run several instance of wget

one of them have PID number 8426

some times later…

I check if PID 8426 is running, it is so my php script react and don't check file downloaded but the fact is that PID 8426 of wget is over and it's another program that running pid 8426.

If the new program run for a long time (eg: a service) I can wait a long time for my php script to check the downloaded file.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
eephyne
  • 911
  • 9
  • 15
  • There is simply no reliable way to identify programs by PID. – Carey Gregory Jul 26 '14 at 15:33
  • if you are launching the process, why not wait for the program to complete? what is your programming language? – Jayan Jul 26 '14 at 16:35
  • Expanding the comment by @CareyGregory, the reason for this is the system may report multiple pids for any process, the running and defunct pids. – David C. Rankin Jul 26 '14 at 20:01
  • @Jayan I can't , the process can last several minutes to hours so can't make php script synchronous (because that freeze the script). – eephyne Jul 27 '14 at 04:38
  • @DavidC.Rankin that mean what. that even my pid can change during their lives ? – eephyne Jul 27 '14 at 04:40
  • Yes, take for example firefox. Right now on my box, `pidof firefox` returns `2383 2364`. Checking `ps axf | grep firefox` shows **2364 /usr/lib64/firefox/firefox** and then **2383 [firefox] **. Many times if a program encounters an error condition, it can try to resolve it automatically by calling SigHUP to restart. In that case the old PID is dead and the program continues under a new one. – David C. Rankin Jul 27 '14 at 08:45

2 Answers2

0

Have you tried employing an object-oriented paradigm, where you could encapsulate the specific PID number into its specific object (i.e., specific program)? To accomplish this, you need to create a class (say you give it the arbitrary name "SOURCE") from which these programs can be obtained as objects belonging to that class. Doing so will encapsulate any information (e.g., PID), including the methods of that specific program to that program alone and, therefore, provide a safer way than doing a hash. Similar methods can be found in the object-oriented programming paradigm of Python.

warship
  • 2,924
  • 6
  • 39
  • 65
  • the pid is one thing but that don't say how to isolate it from another executable that use the same pid later. Using a object to store it or any other method – eephyne Jul 27 '14 at 04:41
  • @eephyne Assigning a class will mask your pid in one object from the identical pid of another object. Thus, the executable would be using a different object even if the pid is the same. In other words, once your executable operates on one pid (object A), it will not operate on the same numerical pid (which would belong to object B) since the executable would recognize that the two objects (even though the pid within the object is identical). A similar matter was brought up here: http://stackoverflow.com/questions/21571121/how-to-tell-the-difference-between-two-different-but-identical-objects – warship Jul 27 '14 at 05:22
  • I don't really see how to adapt this to my use. each time the php script it executed it scan a file to get all the pids he must check so even if I create an object at the moment it don't match the way you mean. – eephyne Jul 27 '14 at 05:50
  • The following site http://php.net/manual/en/language.oop5.basic.php explains how to create an instance (object) of your class. Can you create a user-defined class? – warship Jul 27 '14 at 05:52
0

You can read the binary file that /proc/<pid>/exe points to. The following concept is done in a shell but probably can do that in any language including php:

$ readlink "/proc/$$/exe"
/bin/bash
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • what if it's the same executable ? I mentionned hashing cmdline because it's more accurate than exe but there always a risk – eephyne Jul 27 '14 at 04:36