I do start a long-running background process within a Bash script. I save the PID number inside a variable after sending the process to background and I use that PID number to kill that process when necessary.
However, if that background process terminates somehow before my script kills it and system assigns the same PID number to a newly created process, when I use that number to kill that background process, this action would probably kill that newly created process (depending on permissions, of course).
A used PID number would not be assigned to any newly created process in a short time, I'm aware of that, but my script is running for weeks, so it's possible.
How can I prevent such an accident from happening?
kill PID
, just try the more explicitpkill <name>
command. Would this solve your problem?/proc/PID/environ
, or maybe just checking/proc/PID/exe
is sufficient?