如何通过在 Mac OS X 中给出进程名称来获取进程的 PID? [英] How to get the PID of a process by giving the process name in Mac OS X ?

查看:47
本文介绍了如何通过在 Mac OS X 中给出进程名称来获取进程的 PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来监视任何给定进程的 CPU 和 MEM.为此,我需要将要监视的进程的名称作为命令行参数发送.例如.

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.

./monitorscript <pname>

我需要在脚本中获取进程的 pid 以便我可以在里面使用 ps -p .

I need to get the pid of the process in the script so that i can use a ps -p <pid> inside.

如何根据进程名称获取进程的 pid?

How do i get the pid of a process given its process name?

我知道可能有多个同名的进程.我只想从该列表中取出第一个进程.

I understand that there might be multiple processes in the same name. I just want to get the first process out of that list.

推荐答案

上面的答案大部分是正确的,只是需要针对 Mac OSX 中的不同参数进行一些调整.

The answer above was mostly correct, just needed some tweaking for the different parameters in Mac OSX.

ps -A | grep [f]irefox | awk '{print $1}'

这篇关于如何通过在 Mac OS X 中给出进程名称来获取进程的 PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆