简体   繁体   English

如何使用sudo运行命令的pid

[英]How to get the pid of command running with sudo

我想要得到这个命令的pid。

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

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. 得到最后一个后台进程的pid(在这种情况下将是sudo), ps --ppid来了解它的子ps --ppid 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. 如果您在脚本中执行此操作,则可能需要在sudops之间使用sleep 1以确保子项启动。

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. 请注意,如果你真的必须使用-b标志来sudo,这将无法工作,因为这将导致sudo执行额外的fork并立即退出,丢失了child和parent之间的连接(tcpdump命令将被重新授予init ),这意味着你没有简单的方法来区分孩子和任何其他类似的命令。

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. 这里的其他答案依赖于grepping ps输出。 If there's multiple tcpdump commands running, you may accidentally grep the wrong pid. 如果有多个tcpdump命令在运行,您可能会意外地弄错了错误的pid。 This gets the actual pid and puts it in a file. 这将获得实际的pid并将其放入文件中。

Here's an example running tcpdump as root: 以下是以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"
$

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 只抓住pID我更喜欢使用awk

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

which would return simply 这将简单地返回

11803 11803

I could kill the program from awk as well by piping a kill command to bash 我也可以通过将一个kill命令传递给bash来从awk杀死程序

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

The -o option to ps lets you choose what fields to display. ps-o选项允许您选择要显示的字段。 Of those fields, you can show things like cumulative cpu time ( cputime ), elapsed time ( etime ), and start time ( lstart ). 在这些字段中,您可以显示累积CPU时间( cputime ),已用时间( etime )和开始时间( lstart )等内容。 You can also sort on a field using --sort . 您还可以使用--sort对字段进行--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. 你甚至不需要告诉ps显示你想要排序的字段。 man ps for more details. man ps了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM