lsof 将显示一个表,其中第一列是命令名称,每行是打开的文件。因此,如果要仅显示来自特定命令(例如“java”)的行,尝试 是有意义的lsof | grep ^java
。这有效,但我知道 lsof 有自己的标志来将输出限制为仅特定命令 - -c
。但当我尝试时,lsof -c java
输出完全不同。该-c
方法仅输出大约 10 行,而该grep
方法输出数千行。
所以我的问题是——还有什么可以lsof -c
过滤掉?
由于这个问题被否决了,我将详细说明。提出这个问题的原因是 grep 方法lsof | grep ^java
理论上应该与 的输出相同lsof -c java
。我的理由是:
lsof | grep ^java
将只显示以字母“java”开头的行,并且 lsof 输出的每一行的开头都是打开文件的命令的名称。
lsof -c java
应该只显示由以字母“java”开头的命令打开的文件。命令名称位于 lsof 输出的每一行的开头。
我不知道如何才能更清楚地解释这一点。
为了回答 yoonix 的评论,这是 lsof 手册页中关于 -c 标志的说法。在建议我阅读它以得到答案之前,你应该先阅读它:
-c c selects the listing of files for processes executing the command that begins with the characters of c. Multiple commands may be
specified, using multiple -c options. They are joined in a single ORed set before participating in AND option selection.
If c begins with a `^', then the following characters specify a command name whose processes are to be ignored (excluded.)
If c begins and ends with a slash ('/'), the characters between the slashes are interpreted as a regular expression. Shell
meta-characters in the regular expression must be quoted to prevent their interpretation by the shell. The closing slash may be
followed by these modifiers:
b the regular expression is a basic one.
i ignore the case of letters.
x the regular expression is an extended one
(default).
See the lsof FAQ (The FAQ section gives its location.) for more information on basic and extended regular expressions.
The simple command specification is tested first. If that test fails, the command regular expression is applied. If the simple
command test succeeds, the command regular expression test isn't made. This may result in ``no command found for regex:'' mes‐
sages when lsof's -V option is specified.
答案1
该命令lsof | grep php
将过滤命令输出中的所有行,lsof
其中包含php
行中任何位置的子字符串,如下所示:
apache2 26964 /usr/lib/php/20151012/wddx.so
apache2 26964 /usr/lib/php/20151012/tokenizer.so
apache2 26964 /usr/lib/php/20151012/sysvshm.so
apache2 26964 /usr/lib/php/20151012/sysvsem.so
其中lsof -c php
选择执行以字符开头的命令的进程的文件列表php
:
php-fpm7. 1775 /lib/x86_64-linux-gnu/libpthread-2.23.so
php-fpm7. 1775 /lib/x86_64-linux-gnu/libc-2.23.so
php-fpm7. 1775 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
php-fpm7. 1775 /lib/x86_64-linux-gnu/libssl.so.1.0.0
php-fpm7. 1775 /usr/lib/x86_64-linux-gnu/libxml2.so.2.9.4
希望从给出的例子中可以清楚地了解不匹配的原因。