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
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.
我理解在同一个名称中可能有多个进程。我只是想从这个列表中得到第一个过程。
8 个解决方案
#1
50
The answer above was mostly correct, just needed some tweaking for the different parameters in Mac OSX.
上面的答案基本上是正确的,只是需要对Mac OSX中的不同参数进行一些调整。
ps -A | grep -m1 firefox | awk '{print $1}'
#3
14
You can use the pgrep command like in the following example
您可以在下面的示例中使用pgrep命令。
$ pgrep Keychain\ Access
44186
#4
4
This is the shortest command I could find that does the job:
这是我能找到的最短的命令:
ps -ax | awk '/[t]he_app_name/{print $1}'
Putting brackets around the first letter stops awk from finding the awk process itself.
在第一个字母周围放上括号,可以阻止awk找到awk进程本身。
#5
2
Try this one:
试试这个:
echo "$(ps -ceo pid=,comm= | awk '/firefox/ { print $1; 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)并在第一次匹配后退出。
#6
2
This solution matches the process name more strictly:
此解决方案更严格地匹配流程名称:
ps -Ac -o pid,comm | awk '/^ *[0-9]+ Dropbox$/ {print $1}'
This solution has the following advantages:
该解决方案具有以下优点:
- it ignores command line arguments like
tail -f ~/Dropbox
- 它忽略了像tail -f ~/Dropbox这样的命令行参数。
- it ignores processes inside a directory like
~/Dropbox/foo.sh
- 它忽略了一个目录中的进程,比如~/Dropbox/foo.sh。
- it ignores processes with names like
~/DropboxUID.sh
- 它忽略诸如~/DropboxUID.sh这样的名称。
#7
1
You can try this
你可以试试这个
pid=$(ps -o pid=,comm= | grep -m1 $procname | cut -d' ' -f1)
#8
-2
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的使用对所有进程进行排序。
#1
50
The answer above was mostly correct, just needed some tweaking for the different parameters in Mac OSX.
上面的答案基本上是正确的,只是需要对Mac OSX中的不同参数进行一些调整。
ps -A | grep -m1 firefox | awk '{print $1}'
#2
#3
14
You can use the pgrep command like in the following example
您可以在下面的示例中使用pgrep命令。
$ pgrep Keychain\ Access
44186
#4
4
This is the shortest command I could find that does the job:
这是我能找到的最短的命令:
ps -ax | awk '/[t]he_app_name/{print $1}'
Putting brackets around the first letter stops awk from finding the awk process itself.
在第一个字母周围放上括号,可以阻止awk找到awk进程本身。
#5
2
Try this one:
试试这个:
echo "$(ps -ceo pid=,comm= | awk '/firefox/ { print $1; 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)并在第一次匹配后退出。
#6
2
This solution matches the process name more strictly:
此解决方案更严格地匹配流程名称:
ps -Ac -o pid,comm | awk '/^ *[0-9]+ Dropbox$/ {print $1}'
This solution has the following advantages:
该解决方案具有以下优点:
- it ignores command line arguments like
tail -f ~/Dropbox
- 它忽略了像tail -f ~/Dropbox这样的命令行参数。
- it ignores processes inside a directory like
~/Dropbox/foo.sh
- 它忽略了一个目录中的进程,比如~/Dropbox/foo.sh。
- it ignores processes with names like
~/DropboxUID.sh
- 它忽略诸如~/DropboxUID.sh这样的名称。
#7
1
You can try this
你可以试试这个
pid=$(ps -o pid=,comm= | grep -m1 $procname | cut -d' ' -f1)
#8
-2
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的使用对所有进程进行排序。