Bash:获取子进程名
Bash: Get child process name
我是 bash 的新手。我有这个小代码:
bash.sh
./mama
mama.cpp
#include <stdlib.h>
int main()
{
system("./shvili");
while(1){}
}
shvili.cpp
int main()
{
while(1){}
}
它显示 mama
是 shvili
进程的父进程。我有这样的情况,我不知道子进程的确切名称。所以我的问题是,如何从 C++ 中获取子进程的 PID?(获取进程名称对我来说会更舒服)。
您可以尝试使用 pidof 获取特定进程的 pid。
例子
char line[LEN];
FILE *cmd = popen("pidof...", "r");
fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);
pclose(cmd);
一种至少通过 bash 检查的方法,以防 "situation like this where I don't know exactly the name of child process" 但知道假设为唯一的 parent 进程的名称,可以在 bash
中使用基于 ps
、grep
、sed
(为较小的 pid 删除前导 space)、tr
(将多个连续的 space 压缩到一)和 cut
:
$> cat foo_bar.sh
#! /bin/bash
sleep 120
$> ./foo_bar.sh &
[1] 89239
$> ps -eo pid,ppid,args|grep -e " $(ps -eo pid,args| grep foo_bar.sh| grep -v grep| sed s/^\ //g |cut -f 1 -d ' ') "|grep -v foo_bar.sh| sed s/^\ //g | tr -s ' ' | cut -f 1,3 -d ' '
89241 sleep
因此 parent 进程的唯一名称用于确定 parent pid:
$> ps -eo pid,args| grep foo_bar.sh| grep -v grep| sed s/^\ //g |cut -f 1 -d ' '
89239
这在子进程 ($(...)
) 中评估用于 grep 来自另一个 ps
调用的正确行以确定 child 和名称的寻求 pid(没有额外的参数并且事先不知道 child 的名字。
注意 - 在 bash 中,一些 space 很重要 - 在搜索模式末尾添加 space:
... grep " $(ps -eo pid,args| grep foo_bar.sh| grep -v grep| cut -f 1 -d ' ') " ...
这有助于避免误报,例如当 parent pid 为 123 时,如果不使用 space 进行填充,这将匹配许多包含这些数字的 pid,例如 1234、12345、1123,.. .
Update(对评论做出反应):如果 parent 进程是 a_mama
(分叉 a_shvili
子进程)并且它是机器上唯一具有该名称的进程,那么以下应该有效:
$> p_proc="a_mama"
$> ps -eo pid,ppid,args|grep -e " $(ps -eo pid,args| grep ${p_proc}| grep -v grep| sed s/^\ //g | cut -f 1 -d ' ') "|grep -v ${p_proc}| sed s/^\ //g | tr -s ' ' | cut -f 1,3 -d ' '
12346 a_shvili
你知道 child 进程的名称是 shvili
,你怎么能启动它却不知道它的名称。
如果您知道 child 的 PID
,则不要使用 system
。 system
程序性能不佳,请勿使用。
改为使用 fork
和 exec
:
在parent中做:
#include <unistd.h>
int child_pid = fork();
if (child_pid == -1) {
//fork failed do something about it
} else if (child_pid == 0) {
//this runs for child
execl("shvili", NULL);
//if you get here then exec errored
} else {
//This runs for parent
//child_pid has child pid
//Add parent code hear.
wait(…);
}
解释:
- ifs 的第一个分支在分叉错误时运行。
- 其他
- 在 2 个独立的进程中:
- 第二个分支运行 child。
- 第三个分支运行 parent。
我是 bash 的新手。我有这个小代码:
bash.sh
./mama
mama.cpp
#include <stdlib.h>
int main()
{
system("./shvili");
while(1){}
}
shvili.cpp
int main()
{
while(1){}
}
它显示 mama
是 shvili
进程的父进程。我有这样的情况,我不知道子进程的确切名称。所以我的问题是,如何从 C++ 中获取子进程的 PID?(获取进程名称对我来说会更舒服)。
您可以尝试使用 pidof 获取特定进程的 pid。
例子
char line[LEN];
FILE *cmd = popen("pidof...", "r");
fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);
pclose(cmd);
一种至少通过 bash 检查的方法,以防 "situation like this where I don't know exactly the name of child process" 但知道假设为唯一的 parent 进程的名称,可以在 bash
中使用基于 ps
、grep
、sed
(为较小的 pid 删除前导 space)、tr
(将多个连续的 space 压缩到一)和 cut
:
$> cat foo_bar.sh
#! /bin/bash
sleep 120
$> ./foo_bar.sh &
[1] 89239
$> ps -eo pid,ppid,args|grep -e " $(ps -eo pid,args| grep foo_bar.sh| grep -v grep| sed s/^\ //g |cut -f 1 -d ' ') "|grep -v foo_bar.sh| sed s/^\ //g | tr -s ' ' | cut -f 1,3 -d ' '
89241 sleep
因此 parent 进程的唯一名称用于确定 parent pid:
$> ps -eo pid,args| grep foo_bar.sh| grep -v grep| sed s/^\ //g |cut -f 1 -d ' '
89239
这在子进程 ($(...)
) 中评估用于 grep 来自另一个 ps
调用的正确行以确定 child 和名称的寻求 pid(没有额外的参数并且事先不知道 child 的名字。
注意 - 在 bash 中,一些 space 很重要 - 在搜索模式末尾添加 space:
... grep " $(ps -eo pid,args| grep foo_bar.sh| grep -v grep| cut -f 1 -d ' ') " ...
这有助于避免误报,例如当 parent pid 为 123 时,如果不使用 space 进行填充,这将匹配许多包含这些数字的 pid,例如 1234、12345、1123,.. .
Update(对评论做出反应):如果 parent 进程是 a_mama
(分叉 a_shvili
子进程)并且它是机器上唯一具有该名称的进程,那么以下应该有效:
$> p_proc="a_mama"
$> ps -eo pid,ppid,args|grep -e " $(ps -eo pid,args| grep ${p_proc}| grep -v grep| sed s/^\ //g | cut -f 1 -d ' ') "|grep -v ${p_proc}| sed s/^\ //g | tr -s ' ' | cut -f 1,3 -d ' '
12346 a_shvili
你知道 child 进程的名称是 shvili
,你怎么能启动它却不知道它的名称。
如果您知道 child 的 PID
,则不要使用 system
。 system
程序性能不佳,请勿使用。
改为使用 fork
和 exec
:
在parent中做:
#include <unistd.h>
int child_pid = fork();
if (child_pid == -1) {
//fork failed do something about it
} else if (child_pid == 0) {
//this runs for child
execl("shvili", NULL);
//if you get here then exec errored
} else {
//This runs for parent
//child_pid has child pid
//Add parent code hear.
wait(…);
}
解释:
- ifs 的第一个分支在分叉错误时运行。
- 其他
- 在 2 个独立的进程中:
- 第二个分支运行 child。
- 第三个分支运行 parent。