bash 如何通过在 Mac OS X 中给出进程名称来获取进程的 PID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11546765/
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
How to get the PID of a process by giving the process name in Mac OS X ?
提问by Pradep
I am writing a script to monitor the CPU and MEM of any given process. For that i need to send in the name of the process to be monitored as a commandline argument. For example.
我正在编写一个脚本来监视任何给定进程的 CPU 和 MEM。为此,我需要将要监视的进程的名称作为命令行参数发送。例如。
./monitorscript <pname>
I need to get the pid of the process in the script so that i can use a ps -p <pid>
inside.
我需要在脚本中获取进程的pid,以便我可以使用ps -p <pid>
内部。
How do i get the pid of a process given its process name?
给定进程名称,如何获取进程的 pid?
I understand that there might be multiple processes in the same name. I just want to get the first process out of that list.
我知道可能有多个同名的进程。我只想从该列表中取出第一个进程。
回答by Vijay C
The answer above was mostly correct, just needed some tweaking for the different parameters in Mac OSX.
上面的答案大部分是正确的,只需要对 Mac OSX 中的不同参数进行一些调整。
ps -A | grep [f]irefox | awk '{print }'
回答by bergercookie
You can use the pgrep command like in the following example
您可以使用 pgrep 命令,如下例所示
$ pgrep Keychain\ Access
44186
回答by hgascon
回答by phatmann
This is the shortest command I could find that does the job:
这是我能找到的最短的命令:
ps -ax | awk '/[t]he_app_name/{print }'
Putting brackets around the first letter stops awk from finding the awk process itself.
将括号放在第一个字母周围会阻止 awk 找到 awk 进程本身。
回答by stepmuel
This solution matches the process name more strictly:
此解决方案更严格地匹配进程名称:
ps -Ac -o pid,comm | awk '/^ *[0-9]+ Dropbox$/ {print }'
This solution has the following advantages:
该解决方案具有以下优点:
- it ignores command line arguments like
tail -f ~/Dropbox
- it ignores processes inside a directory like
~/Dropbox/foo.sh
- it ignores processes with names like
~/DropboxUID.sh
- 它忽略命令行参数,如
tail -f ~/Dropbox
- 它忽略目录内的进程,如
~/Dropbox/foo.sh
- 它忽略名称如下的进程
~/DropboxUID.sh
回答by Nicholas Riley
Try this one:
试试这个:
echo "$(ps -ceo pid=,comm= | awk '/firefox/ { print ; exit }')"
The ps
command produces output like this, with the PID in the first column and the executable name (only) in the second column:
该ps
命令会产生这样的输出,第一列是 PID,第二列是可执行文件名称(仅):
bookworm% ps -ceo pid=,comm=
1 launchd
10 kextd
11 UserEventAgent
12 mDNSResponder
13 opendirectoryd
14 notifyd
15 configd
...which awk
processes, printing the first column (pid) and exiting after the first match.
...哪个awk
进程,打印第一列(pid)并在第一次匹配后退出。
回答by chepner
You can try this
你可以试试这个
pid=$(ps -o pid=,comm= | grep -m1 $procname | cut -d' ' -f1)
回答by Ape
Why don't you run TOP and use the options to sort by other metrics, other than PID? Like, highest used PID from the CPU/MEM?
为什么不运行 TOP 并使用选项按除 PID 之外的其他指标进行排序?比如,来自 CPU/MEM 的最高使用 PID?
top -o cpu <---sorts all processes by CPU Usage
top -o cpu <---按 CPU 使用率对所有进程进行排序