What's a proper way of checking if a PID is running?
for most linux distros enumerating the /proc/{pid} is a good way to obtain information about the running processes, and usually how the userspace commands like "ps" are communicating with the kernel. So for example you can do;
[ -d "/proc/${kpid}" ] && echo "process exists" || echo "process not exists"
Edit: you should check that kpid is set, but this is more useful as it will return "not exists" for unset ${kpid}
[ -n "${kpid}" -a -d "/proc/${kpid}" ] && echo "process exists" || echo "process not exists"
As Anders noted, you should use kill -0
for POSIX compliance.
On Linux systems, you can also check for the existence of a file in the /proc filesystem, e.g.,
-f /proc/$(cat something.pid)/status