如何使用PID号码在Linux中查找进程名称

在本文中,我们将讨论如何通过进程标识号(PID)找到进程名称。在我们深入探讨实际解决方案之前,让我们简要谈谈Linux如何创建和标识进程。

每当用户或系统(Linux)启动一个程序时,内核都会创建一个进程。进程在内存中保存程序的执行细节,比如输入输出数据、变量等等。

重要的是,由于Linux是一个多任务操作系统,它同时执行多个程序,这意味着每个进程必须被特定地标识。

内核使用进程标识号(PID)来标识每个进程,每个进程实例必须有一个与其他进程不同的唯一PID,在调用进程时分配,以避免任何执行错误。

/proc文件系统存储有关系统上当前运行的进程的信息,它包含每个进程的目录。

使用ls命令列出其内容,然而,列表可能很长,所以使用管道和less工具以更方便的方式查看/proc内容,如下所示:

$ ls /proc 
OR
$ ls /proc | less 
List /proc File System
1     168   2230  25    329   584   7386  83         driver        schedstat
10    169   2234  2503  33    603   74    830        execdomains   scsi
1070  17    2247  2507  34    610   7411  833        fb            self
1081  1702  2256  2523  349   611   7423  836        filesystems   slabinfo
109   1714  2258  253   35    612   745   839        fs            softirqs
11    173   2266  2551  36    613   746   84         interrupts    stat
110   1760  2273  26    362   62    75    844        iomem         swaps
1188  1763  2278  2688  3642  63    7533  85         ioports       sys
12    1769  2282  2694  3643  64    7589  86         irq           sysrq-trigger
1204  177   2283  2695  37    6436  76    860        kallsyms      sysvipc
1209  1773  2285  2698  38    65    7619  87         kcore         thread-self
1254  18    2287  2699  39    66    7689  9          keys          timer_list
13    1847  2295  27    3974  67    7690  94         key-users     timer_stats
15    1914  23    2702  3976  68    77    977        kmsg          tty
152   1917  2308  28    4273  6897  7725  981        kpagecgroup   uptime
153   1918  2309  280   4374  69    7729  987        kpagecount    version
154   1938  2310  2815  4392  6969  7733  997        kpageflags    version_signature
155   1956  2311  2817  44    6980  78    acpi       loadavg       vmallocinfo
156   1981  2315  282   45    7     79    asound     locks         vmstat
1565  1986  2316  283   4543  70    790   buddyinfo  mdstat        zoneinfo
1567  1988  2317  29    46    71    8     bus        meminfo
157   2     2324  2935  461   7102  80    cgroups    misc
1579  20    2347  2944  4686  72    808   cmdline    modules
158   2010  2354  3     47    73    81    consoles   mounts
1584  2043  2436  30    4700  7304  810   cpuinfo    mtrr
159   2044  2437  3016  5     7311  815   crypto     net
1590  21    2442  31    515   7322  82    devices    pagetypeinfo
16    2167  2443  318   5273  7347  820   diskstats  partitions
160   22    2492  32    5274  7367  823   dma        sched_debug

从上面的屏幕截图可以看出,编号的目录存储了有关正在执行的进程的信息文件,其中每个数字对应一个PID

以下是PID为1的systemd进程的文件列表:

$ ls /proc/1
Show SystemD Process PID
ls: cannot read symbolic link '/proc/1/cwd': Permission denied
ls: cannot read symbolic link '/proc/1/root': Permission denied
ls: cannot read symbolic link '/proc/1/exe': Permission denied
attr        coredump_filter  gid_map    mountinfo   oom_score      schedstat  status
autogroup   cpuset           io         mounts      oom_score_adj  sessionid  syscall
auxv        cwd              limits     mountstats  pagemap        setgroups  task
cgroup      environ          loginuid   net         personality    smaps      timers
clear_refs  exe              map_files  ns          projid_map     stack      uid_map
cmdline     fd               maps       numa_maps   root           stat       wchan
comm        fdinfo           mem        oom_adj     sched          statm

您可以使用传统的Linux命令如pstop和相对较新的glances命令以及其他许多命令来监视进程及其PID,如下面的示例所示:

$ ps aux
Show Running Processes with PID
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0 185728  6268 ?        Ss   10:15   0:01 /sbin/init splash
root         2  0.0  0.0      0     0 ?        S    10:15   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    10:15   0:09 [rcu_sched]
root         8  0.0  0.0      0     0 ?        S    10:15   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/0]
root        10  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/0]
root        11  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/1]
root        12  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/1]
root        13  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/1]
root        15  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/1:0H]
root        16  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/2]
root        17  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/2]
root        18  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/2]
root        20  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/2:0H]
root        21  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/3]
root        22  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/3]
root        23  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/3]
root        25  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/3:0H]
root        26  0.0  0.0      0     0 ?        S    10:15   0:00 [kdevtmpfs]
root        27  0.0  0.0      0     0 ?        S<   10:15   0:00 [netns]
root        28  0.0  0.0      0     0 ?        S<   10:15   0:00 [perf]
....

使用传统的top命令监视Linux进程。

$ top
Monitor Linux Processes with top Command

使用glances,一个新的实时进程监控工具,监视Linux进程。

$ glances
Glances – Real Time Linux Processes Monitoring

了解更多关于如何在Linux系统中安装Glances。

查找进程PID号码

要查找进程的PID号码,您可以使用pidof命令来打印出进程的PID号码:

$ pidof firefox
$ pidof python
$ pidof cinnamon
Find Linux Process PID

回到我们关注的重点,假设您已经知道一个进程的PID号码,您可以使用下面的命令形式打印出它的名称:

$ ps -p PID -o format 

其中:

  1. -p指定PID号码
  2. -o格式启用用户定义的格式

使用PID号码查找进程名称

在这个部分中,我们将看到如何使用进程的PID号码来查找进程名称,使用用户定义的格式comm=,意思是命令名称,与进程名称相同。

$ ps -p 2523 -o comm=
$ ps -p 2295 -o comm=
Find Linux Process Name

有关其他使用信息和选项,请查阅ps手册

$ man ps

如果您想使用PID号码杀死一个进程,我建议您阅读使用PID号码查找和杀死Linux进程

目前就这些了,如果您知道其他更好的查找进程名称使用PID号码的方法,请在下面的评论部分与我们分享。

Source:
https://www.tecmint.com/find-process-name-pid-number-linux/