如何获取使用 sudo 运行的命令的 pid

发布于 2025-01-06 06:43:54 字数 94 浏览 1 评论 0原文

我正在尝试获取该命令的 pid。

sudo -b tcpdump -i eth0 port 80 -w eth0.pcap

I am trying to get the pid of this command.

sudo -b tcpdump -i eth0 port 80 -w eth0.pcap

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻安 2025-01-13 06:43:54

您可以使用 $! 获取最后一个后台进程的 pid(在本例中为 sudo),并使用 ps --ppid 来了解其子进程。例如:

$ sudo tcpdump -i eth0 port 80 -w eth0.pcap &
$ ps --ppid $! -o pid=
16772
$ ps --pid 16772
  PID TTY          TIME CMD
16772 pts/3    00:00:00 tcpdump

如果您在脚本中执行此操作,您可能需要在 sudops 之间使用 sleep 1 以确保让孩子开始。

请注意,如果您确实必须对 sudo 使用 -b 标志,则这将不起作用,因为这将导致 sudo 进行额外的 fork 并立即退出,从而失去子级和父级之间的连接( tcpdump 命令将被重新设置为 init),这意味着您将无法简单地将子命令与任何其他类似命令区分开来。

You can use $! to get the pid of the last background process (which will be the sudo in this case), and ps --ppid to find out about its children. So for example:

$ sudo tcpdump -i eth0 port 80 -w eth0.pcap &
$ ps --ppid $! -o pid=
16772
$ ps --pid 16772
  PID TTY          TIME CMD
16772 pts/3    00:00:00 tcpdump

If you're doing this in a script, you might want to use a sleep 1 between the sudo and ps to ensure that the child gets started.

Note that if you really must use the -b flag to sudo, this won't work, as that will cause sudo to do an extra fork and immediately exit, losing the connection between child and parent (the tcpdump command will get reparented to init), which means you'll have no easy way of distinguishing the child from any other similar command.

假装不在乎 2025-01-13 06:43:54

这是一种方法:

sudo -u username sh -c "echo \$\$ > /tmp/my_pid/file; exec my_command" &

这里的其他答案依赖于 grep ps 输出。如果有多个 tcpdump 命令正在运行,您可能会意外地 grep 到错误的 pid。这会获取实际的 pid 并将其放入文件中。

下面是一个以 root 身份运行 tcpdump 的示例:

 $ sudo -u root sh -c "echo \$\$ > /tmp/tcpdump.pid; exec tcpdump -i en3 -w eth0.pcap" &
[1] 37201
tcpdump: listening on en3, link-type EN10MB (Ethernet), capture size 65535 bytes
$ sudo kill `cat /tmp/tcpdump.pid`
6212 packets captured
6243 packets received by filter
0 packets dropped by kernel
[1]+  Done                    sudo -u root sh -c "echo \$\$ > /tmp/tcpdump.pid; exec tcpdump -i en3 -w eth0.pcap"
$

Here's one way to do it:

sudo -u username sh -c "echo \$\$ > /tmp/my_pid/file; exec my_command" &

The other answers here rely on grepping ps output. If there's multiple tcpdump commands running, you may accidentally grep the wrong pid. This gets the actual pid and puts it in a file.

Here's an example running tcpdump as root:

 $ sudo -u root sh -c "echo \$\$ > /tmp/tcpdump.pid; exec tcpdump -i en3 -w eth0.pcap" &
[1] 37201
tcpdump: listening on en3, link-type EN10MB (Ethernet), capture size 65535 bytes
$ sudo kill `cat /tmp/tcpdump.pid`
6212 packets captured
6243 packets received by filter
0 packets dropped by kernel
[1]+  Done                    sudo -u root sh -c "echo \$\$ > /tmp/tcpdump.pid; exec tcpdump -i en3 -w eth0.pcap"
$
命硬 2025-01-13 06:43:54

为此,我将输入

sudo gvim &

ps aux | grep gvim

为我提供以下输出

root 11803 0.0 0.0 12064 2776 pts/3 T 12:17 0:00 sudo gvim

只获取我更喜欢使用 awk

辅助 | awk '/gvim/ {print $2}'

会简单地返回

<块引用>

11803

来从 awk 终止程序

我也可以通过将终止命令传送到 bash ps aux | awk '/gvim/ {print "sudo Kill -9 "$2}' | bash

for this purpose I will enter

sudo gvim &

ps aux | grep gvim

supplies me with the following output

root 11803 0.0 0.0 12064 2776 pts/3 T 12:17 0:00 sudo gvim

to grab only the pID i prefer to use awk

ps aux | awk '/gvim/ {print $2}'

which would return simply

11803

I could kill the program from awk as well by piping a kill command to bash

ps aux | awk '/gvim/ {print "sudo kill -9 "$2}' | bash

夕嗳→ 2025-01-13 06:43:54

ps-o 选项允许您选择要显示的字段。在这些字段中,您可以显示累积 CPU 时间 (cputime)、已用时间 (etime) 和开始时间 (lstart) 等内容。您还可以使用 --sort 对字段进行排序。因此,您的解决方案可能是:

ps -eo pid,command,lstart --sort lstart | grep 'sudo -b tcpdump' | tail -1

您甚至不需要告诉 ps 显示您想要排序的字段。 man ps 了解更多详细信息。

The -o option to ps lets you choose what fields to display. Of those fields, you can show things like cumulative cpu time (cputime), elapsed time (etime), and start time (lstart). You can also sort on a field using --sort. So a solution for you could be:

ps -eo pid,command,lstart --sort lstart | grep 'sudo -b tcpdump' | tail -1

You don't even need to tell ps to display the field you want to sort by. man ps for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文