linux pidof用法,初学者的Linux pidof命令教程(5个示例)

初学者的Linux pidof命令教程(5个示例)

Linux命令行提供了很多与进程配合使用的实用程序。 一旦这样的工具是pidof ,它就像名字所暗示的那样给你一个已经执行的进程的进程ID。 在本教程中,我们将使用一些易于理解的示例来讨论pidof的基础知识。

但在这之前,值得一提的是,这里的所有示例都已在Ubuntu 16.04 LTS机器上进行了测试。

Linux pidof命令

如上所述,pidof命令可让您查找正在运行的程序的进程ID。 以下是它的语法:

pidof [options] command

以下是手册页上关于该工具的内容:Pidof finds the process id's (pids) of the named  programs.  It  prints

those id's on the standard output. This program is on some systems used

in run-level change scripts, especially when the system has a  System-V

like   rc  structure.  In  that  case  these  scripts  are  located  in

/etc/rc?.d, where ? is the runlevel. If the system  has  a  start-stop-

daemon (8) program that should be used instead.

以下是一些问答式样的例子,应该给你一个关于工具如何工作的好主意。

Q1。 如何使用pidof命令?

基本用法很简单 - 只需将程序的名称作为输入传递给命令即可。 例如:

pidof gedit

0f12a7cc06656b263fac44a39e66b4fd.png

因此,您可以在输出中看到生成gedit进程ID的命令。

Q2。 如何让pidof只返回单个pid?

有时候,你会看到pidof命令返回多个pid。 例如,尝试使用Web浏览器程序作为输入来运行pidof命令。

pidof firefox

以下是我的系统上生成的输出:

943f377e9a6a3aaff7be3169c6ee9a6c.png

但是,如果您需要,可以强制该工具仅在输出中生成一个PID。 这可以使用-s命令行选项完成。

pidof -s firefox

f7f11ba747713dfe5e6cb94ea0f3b5b9.png

Q3。 如何根据根目录限制输出?

如果您希望pidof仅返回使用相同根目录运行的进程ID,请使用-c命令行选项。

pidof -c program_name

请注意,非root用户忽略此选项,因为他们将无法检查进程的当前根目录

他们不拥有。

Q4。 在网络文件系统中保存二进制文件的情况下如何使用pidof?

在处理位于基于网络的文件系统(如NFS)上的二进制文件时,可以使用-n命令行选项使pidof命令避免使用stat系统函数调用。

pidof -n program_name

请注意,手册页建议“不使用此选项,而是可以设置和导出变量PIDOF_NETFS。”

Q5。 pidof如何在内部工作?

官方文档称pidof实际上与killall是相同的程序。 该程序根据其被调用的名称行为。 以下是手册页中的内容:When pidof is invoked with a full pathname to the program it should

find the pid of, it is reasonably safe. Otherwise it is  possible  that

it  returns  pids of running programs that happen to have the same name

as the program you're after but are actually other programs. Note  that

that  the executable name of running processes is calculated with read?

link(2), so symbolic links to executables will also match.

结论

在许多情况下,pidof命令可能派上用场。 例如,如果你想杀死一个PID你不知道的进程,你可以快速使用这个工具来查找进程ID,并且该进程可以在很短的时间内被杀死。 pidof命令不提供很多选项,我们已经在这里讨论了其中的大部分。 有关更多信息,请转至该工具的手册页 。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值