Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It seems many programmers are happy with saving a PID to a file, and then later read and use the PID as if it was guaranteed to be the same process. It seems the general idea is that the odds are good enough to ignore. How can I simply demonstrate this problem? Ideally in such a way that existing shell scripts can be adapted to demonstrate this with the minimum of trial and error.

Dummy example:

foo &
echo $pid > pidfile
    kill $!
    sleep 1 &
until [ $pid -eq $! ]
kill "$(cat pidfile)" # Kills `sleep`, not `foo`!
share|improve this question
To end up killing the wrong process you need 1) a process that uses a PID file to die without removing the PID file, 2) the process not be restarted by systemd/init/launchd/whatever, which would put a new PID in the PID file, 3) 100,000 PIDs have been assigned so that PIDs start being reused, 4) a kill() system call directed at the PID of that old process while a new process is using the PID. If the user running the process isn't root, the odds of killing the wrong process decrease even further. There's no way to orchestrate these conditions easily on a live system. – Kyle Jones Apr 24 '13 at 1:05
@KyleJones On Ubuntu 12.04 at least /proc/sys/kernel/pid_max contains 32768, so 100,000 is an overestimation. And I don't see how point 4 is relevant - I've seen daemons which will kill the value in the PID file when stopping or restarting them. – l0b0 Apr 24 '13 at 6:55

Using exec is a pretty good demonstration of PID reuse:


cat > foo << 'EOF'
echo "Inside foo."
sleep 5
exec ./bar

cat > bar << 'EOF'

echo "Inside bar."
sleep 5

chmod a+x foo bar

./foo &

while sleep 3; do
    [[ -f "/proc/$!/cmdline" ]] || break
    printf 'pid %d == %s\n' "$!" "$(tr '\0' ' ' < "/proc/$!/cmdline")"

rm foo bar

Running this script yields something like the following:

$ ./script
Inside foo.
pid 4953 == /bin/bash ./script
Inside bar.
pid 4953 == /bin/bash /tmp/tmp.AvDLtMWYPy/bar
pid 4953 == /bin/bash /tmp/tmp.AvDLtMWYPy/bar

You can exec arbitrary programs, so there is no guarantee (at all) that the process at that PID is the same, or even similar.

share|improve this answer
How is this a demonstration of PID reuse? exec is specifically designed to execute a command in place of this shell without creating a new process. You just have printed the PID of the same process 3 times, no wonder it's the same. – Dmitry Grigoryev Oct 28 '15 at 12:19
@DmitryGrigoryev exec entirely replaces the process image. Case in point, some systems use it instead of reaping/forking children for short lived processes -- storing PID data there is useless, because it only applies to an unknown time period which may have passed. In the question, almost certainly the word "process" is being used to mean "process image". – Chris Down Oct 28 '15 at 12:31
OK, then I guess the question doesn't make much sense. Like you said, storing PID data is useless in such cases. – Dmitry Grigoryev Oct 28 '15 at 12:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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