Linux 检查进程是否存在给定其 pid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9152979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:24:19  来源:igfitidea点击:

Check if process exists given its pid

clinuxpid

提问by Simone

Given the pid of a Linux process, I want to check, from a C program, if the process is still running.

给定 Linux 进程的 pid,我想从 C 程序中检查该进程是否仍在运行。

采纳答案by Blagovest Buyukliev

Issue a kill(2)system call with 0as the signal. If the call succeeds, it means that a process with this pid exists.

发出一个kill(2)系统调用0作为信号。如果调用成功,则说明存在具有该pid的进程。

If the call fails and errnois set to ESRCH, a process with such a pid does not exist.

如果调用失败并errno设置为ESRCH,则不存在具有此类 pid 的进程。

Quoting the POSIX standard:

引用 POSIX 标准:

If sig is 0 (the null signal), error checking is performed but no signal is actually sent. The null signal can be used to check the validity of pid.

如果 sig 为 0(空信号),则执行错误检查但实际上不发送任何信号。空信号可用于检查 pid 的有效性。

Note that you are not safe from race conditions: it is possible that the target process has exited and another process with the same pid has been started in the meantime. Or the process may exit very quickly after you check it, and you could do a decision based on outdated information.

请注意,您无法避免竞争条件:目标进程可能已退出,同时另一个具有相同 pid 的进程已启动。或者进程在你检查后可能会很快退出,你可以根据过时的信息做出决定。

Only if the given pid is of a child process (fork'ed from the current one), you can use waitpid(2)with the WNOHANGoption, or try to catch SIGCHLDsignals. These are safe from race conditions, but are only relevant to child processes.

仅当给定的 pid 属于子进程(fork从当前进程中提取)时,您才可以使用waitpid(2)WNOHANG选项,或者尝试捕获SIGCHLD信号。这些在竞争条件下是安全的,但仅与子进程相关。

回答by Janus Troelsen

Use procfs.

使用 procfs。

#include <sys/stat.h>
[...]
struct stat sts;
if (stat("/proc/<pid>", &sts) == -1 && errno == ENOENT) {
  // process doesn't exist
}

Easily portable to

易于携带到

  • Solaris
  • IRIX
  • Tru64 UNIX
  • BSD
  • Linux
  • IBM AIX
  • QNX
  • Plan 9 from Bell Labs
  • 索拉里斯
  • 爱丽丝
  • Tru64 UNIX
  • BSD
  • Linux
  • IBM AIX
  • QNX
  • 贝尔实验室的 Plan 9

回答by Hallvard

You can issue a kill(2)system call with 0as the signal.

您可以kill(2)使用0作为信号发出系统调用。

There's nothing unsafe about kill -0. The program must be aware that the result can become obsolete at any time (including that the pid can get reused before kill is called), that's all. And using procfs instead doesuse the pid too, and doing so in a more cumbersome and nonstandard way.

没有什么不安全的kill -0。程序必须意识到结果可能在任何时候变得过时(包括 pid 可以在调用 kill 之前被重用),仅此而已。而使用 procfs也确实使用了 pid,并且以更麻烦和非标准的方式这样做。

回答by Kinthelt

As an addendum to the /proc filesystem method, you can check the /proc/<pid>/cmdline (assuming it was started from the command line) to see if it is the process you want.

作为 /proc 文件系统方法的补充,您可以检查 /proc/<pid>/cmdline(假设它是从命令行启动的)以查看它是否是您想要的进程。

回答by the paul

kill(pid, 0)is the typical approach, as @blagovest-buyukliev said. But if the process you are checking might be owned by a different user, and you don't want to take the extra steps to check whether errno == ESRCH, it turns out that

kill(pid, 0)正如@blagovest-buyukliev 所说,这是典型的方法。但是,如果您正在检查的进程可能由不同的用户拥有,并且您不想采取额外的步骤来检查是否errno == ESRCH,事实证明

(getpgid(pid) >= 0)

is an effective one-step method for determining if any process has the given PID (since you are allowed to inspect the process group ID even for processes that don't belong to you).

是一种有效的一步法,用于确定是否有任何进程具有给定的 PID(因为即使对于不属于您的进程,您也可以检查进程组 ID)。

回答by Anver Hisham

ps -p $PID > /dev/null 2>&1;   echo $?

This command return 0if process with $PIDis still running. Otherwise it returns 1.

0如果进程$PID仍在运行,则此命令返回。否则返回1

One can use this command in OSX terminal too.

也可以在 OSX 终端中使用此命令。