Can some one please tell me how to check if a unix process with a given process id is running inside a C program. I know I can call system() and use the ps command but I dont want to call the system().
/proc/<PID> should exists if the process PID is still running.
Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.
You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.
The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.
Using kill(2):
if (kill(pid, 0) == 0) {
/* process is running or a zombie */
} else if (errno == ESRCH) {
/* no such process with the given pid is running */
} else {
/* some other error... use perror("...") or strerror(errno) to report */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With