-
ps race condition?
i'm calling ps within a script (written in php, using shell_exec for the call) to figure out if the current script is already running under a different pid. the problem is, like 0.1% of the time, the current process is not listed in ps. the script doesn't see itself.
i am trying to figure out how this is possible. i am doing the ps call right at the beginning of the script. i can't seem to find any info on google on how ps works... is the process list generated instantaneously or is it just updated periodically like top?
i am guessing this is the case because inserting a "sleep(1)" before the call completely alleviates the problem. but i'd really like to know definitively what is going on.
i'm also open to suggestions on a better way to do this operation, i.e. best practices. i'd rather not use an additional file for locking though, if possible.
-
ps is supposed to print a snapshot of the currently active processes at the time that you run it.
What options are you using? I'd recommend ps -efww so you can definitely eliminate the possibility that a truncated command listing is causing the problem.
If sleep has eliminated the problem, that would suggest that the snapshot is from a very small amount of time in the past. I wouldn't describe that as a race condition - just an slightly old snapshot. :)
In support of that theory, I've occasionally seen what you are describing in a different form. Sometimes (rarely) when running ps and grep-ing for a value, the actual ps command I ran does not show up.
-
i'm using ps axww. that makes sense though, about it being an older "snapshot". that's kind of what i figured, but i wanted to see if anyone knew for sure. the "*nix guy" i asked about it originally said it would never happen, but then i asked him again yesterday and told him about the sleep(1) and he changed his mind :) anyway, thanks for the input.