如何使用ps命令监视Linux进程

Linux便携式计算机上的终端窗口。Fatmawati Achmad Zaenuri / Shutterstock

使用ps命令获取Linux计算机中正在运行的进程的快照。通过名称,用户甚至终端来定位进程,并根据需要提供尽可能多的细节。我们向您展示如何。

Linux上的流程管理

所有Linux和类Unix操作系统的心脏都是内核。它的许多职责之一是分配系统资源,例如RAM和CPU时间。这些必须实时处理,以便根据每个任务的优先级公平地共享所有正在运行的进程。

有时,任务可能会锁定,进入紧密循环或由于其他原因而变得无响应。否则它们可能会继续运行,但会占用过多的CPU时间或RAM,或者表现为同样反社会的行为。有时需要杀死任务,以怜悯所有参与其中的人。第一步。当然是确定有问题的过程。

但是也许您根本没有任何任务或性能问题。也许您只是对计算机内部正在运行的进程感到好奇,并且想了解一下这些信息。 ps命令满足这两个需求。它为您提供了“现在”计算机内部正在发生的情况的快照。

ps足够灵活,可以按照您想要的格式为您提供所需的准确信息。实际上,ps有很多选择。此处描述的选项将满足大多数普通需求。如果您需要比本文更深入地了解ps,您会发现我们的介绍使手册页更易于理解。

上市流程

使用ps的最简单方法是不使用任何参数启动它:

ps

ps显示由运行该命令的用户启动的进程的列表。

终端窗口中ps的输出“ width =” 646“ height =” 122“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

这四列是:

  • PID:进程的进程ID号。
  • TTY:用户登录所在的控制台的名称。
  • TIME:进程已使用的CPU处理时间。
  • CMD:启动该过程的命令的名称

所有用户的上市过程

通过添加-e(选择所有进程),我们可以使ps列出所有用户启动的进程,而不仅仅是运行ps命令的用户。由于这将是一长串的清单,因此我们将其缩减为更少。

ps -e |减

ps -e |在终端窗口中减少“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

进程列表被传送到更少的列表中。

ps -e的输出通过管道传输到终端窗口中的更少内容“ width =” 646“ height =” 382“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages“。 loadIfVisibleAndMaybeBeacon(this);

列表中还有更多条目,但我们看到的列与以前相同,为四列。带有问号的条目? TTY列中的“开始”不是从终端窗口启动的。

显示流程层次结构

有时,如果您可以看到哪个进程启动了其他进程,则有助于找出问题或确定特定进程。我们使用-H(层次结构)选项来执行此操作。

ps -eH |减

ps -eH |少在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

缩进指示哪些进程是其他进程的父进程。

在终端窗口中从ps -eH的输出少一些“ width =” 646“ height =” 382“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (这个);

为了更加清晰,我们可以要求ps添加一些ASCII行并将层次结构绘制为树。执行此操作的选项是–forest选项。

ps -eH –forest |减

ps -eH --forest |少在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

这使得更容易跟踪哪些进程是其他进程的父进程。

ps -eH --forest的输出在终端窗口中通过管道传递到更少的位置“ width =” 646“ height =” 382“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed。 lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

按名称列出流程

您可以通过grep将ps的输出传递给名称与搜索词匹配的列表条目。在这里,我们正在寻找与“ firefox”搜索字词匹配的条目:

ps -e | grep Firefox

ps -e |终端窗口中的grep firefox“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

在这种情况下,输出只是我们感兴趣的过程的单个条目。当然,如果我们启动了多个Firefox实例,则列表中将返回多个项。

ps -e的输出|终端窗口中的grep firefox“ width =” 646“ height =” 97“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

在输出中显示更多列

要将更多列添加到输出,请使用-f(完整格式)选项。

ps -ef |减

ps -ef |少在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

ps的输出中包含一组额外的列。

ps -ef的输出|在终端窗口中少“” width =“ 646” height =“ 382” onload =“ pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);” onerror =“ this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

这些列是:

  • UID:此进程的所有者的用户ID。
  • PID:进程的进程ID。
  • PPID:进程的父进程ID。
  • C:该进程具有的子代数。
  • STIME:开始时间。该过程开始的时间。
  • TTY:用户登录所在的控制台的名称。
  • TIME:进程已使用的CPU处理时间。
  • CMD:启动进程的命令的名称。

通过使用-F(超全格式)选项,我们可以获得更多的列:

ps -eF |减

ps -eF |少在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

我们这次获得的列要求将屏幕横向滚动以显示所有内容。

ps -eF的输出|少在终端窗口中,显示的左侧“ width =” 646“ height =” 382“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (这个);

按下“向右箭头”键可将显示向左移动。

ps -eF的输出|少在终端窗口中,显示的右侧“ width =” 646“ height =” 382“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (这个);

我们现在得到的列是:

  • UID:此进程的所有者的用户ID。
  • PID:进程的进程ID。
  • PPID:进程的父进程ID。
  • C:该进程具有的子代数。
  • SZ:过程映像的RAM页中的大小。
  • RSS:居民集大小。这是该进程使用的未交易所的物理内存。
  • PSR:将进程分配给的处理器。
  • STIME:开始时间。该过程开始的时间。
  • TTY:用户登录所在的控制台的名称。
  • TIME:进程已使用的CPU处理时间。
  • CMD:启动进程的命令的名称。

按进程ID列出进程

找到感兴趣的进程的进程ID后,可以将其与ps命令一起使用以列出该进程的详细信息。使用-p(按进程ID选择)选项可以实现以下目的:

ps -p 3403

ps -p 3403在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

列出了此过程的详细信息:

终端窗口中ps -p 3403的输出“ width =” 646“ height =” 122“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this );

您不限于一个进程ID。您可以提供一个进程ID列表,以空格分隔。

按命令列出进程

-C(命令)选项使您可以使用命令名称搜索进程。即,启动进程的命令的名称。这与命令行稍有不同,命令行可能包含路径名,参数或选项。

ps -C快门

ps -C终端窗口中的快门“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

列出了快门处理的详细信息。

列出用户拥有的流程

要查看特定用户拥有的进程,请使用-u(用户列表)选项:

ps -u玛丽

ps -u玛丽在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

显示用户帐户mary拥有的进程。

终端窗口中ps -u mary的输出“ width =” 646“ height =” 197“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this );

终端列出流程

要查看与TTY相关的进程,请使用-t(按TTY选择)选项。 -t选项在不带TTY编号的情况下使用,报告与当前终端窗口关联的进程。

tty

太平洋标准时间

终端窗口中的ps -t“ width =” 646“ height =” 167“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

tty命令报告这是伪整数类型0。ps -t列出的进程都与TTY pts / 0相关联。

如果我们在命令行上传递了一个TTY编号,则应该获得与该TTY相关联的进程的报告。

ps -t 1

ps -t 1 ina终端窗口“ width =” 646“ height =” 132“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

这次所有进程都与TTY pts / 1相关。

相关:什么是Linux上的TTY? (以及如何使用tty命令)

选择要显示的列

使用-o(格式)选项,可以选择要包含在ps输出中的列。您可以按名称指定列。列名的(长)列表可以在手册页上的标题为“标准​​格式说明符''的部分中看到。在此示例中,我们选择具有CPU时间(pcpu)和带有参数(args)的命令行包含在输出中。

ps -e -o pcpu,args |减

ps -e -o pcpu,args |少在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

输出仅包含我们要求的两个列。

ps -e -o pcpu,args的输出|在终端窗口中少“” width =“ 646” height =“ 382” onload =“ pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);” onerror =“ this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

按列对输出排序

您可以使用–sort选项对输出进行排序。让我们按“ CPU”列对输出进行排序:

ps -e -o pcpu,args –sort -pcpu |减

ps -e -o pcpu,args --sort -pcpu |少在终端窗口中“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

pcpu排序参数上的连字符“-”给出降序排序。

ps的输出,在终端窗口中按cpu排序;“ width =” 646“ height =” 382“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this );

要查看十个最占用CPU的进程,请通过head命令通过管道输出:

ps -e -o pcpu,args –sort -pcpu |头-10

ps -e -o pcpu,args --sort -pcpu |终端窗口中的head 10“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

我们得到一个排序的,被截断的列表。

ps -e -o pcpu,args --sort -pcpu的输出|终端窗口中的head 10“ width =” 646“ height =” 262“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

如果我们在显示中添加更多列,则可以按更多列进行排序。让我们添加pmem列。这是该进程正在使用的计算机内存的百分比。如果没有连字符或加号“ +”,则排序顺序将递增。

ps -e -o pcpu,pmem,args –sort -pcpu,pmem |头-10

ps -e -o pcpu,pmem,args --sort -pcpu,pmem |终端窗口中的head 10“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

我们得到了额外的列,并且新列包含在排序中。第一列在第二列之前排序,第二列以升序排序,因为我们没有在pmem上加上连字符。

ps -e -o pcpu,pmem,args --sort -pcpu,pmem的输出|终端窗口中的head 10“ width =” 646“ height =” 292“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

让我们使其更加有用,并在进程ID列(pid)中添加,以便我们可以在列表中看到每个进程的进程号。

ps -e -o pid,pcpu,pmem,args –sort -pcpu,pmem |头-10

ps -e -o pid,pcpu,pmem,args --sort -pcpu,pmem |终端窗口中的头10“ width =” 646“ height =” 77“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

现在我们可以确定流程了。

ps -e -o pid,pcpu,pmem,args --sort -pcpu,pmem的输出|头10“ width =” 646“ height =” 292“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

通过进程ID终止进程

我们介绍了多种方法来识别进程,包括名称,命令,用户和终端。我们还介绍了通过进程的动态属性(例如CPU使用率和内存)识别进程的方法。

因此,我们可以通过一种方式来识别正在运行的进程。通过了解它们的进程ID,我们可以(如果需要)使用kill命令杀死任何这些进程。如果我们想取消898程序,可以使用以下格式:

须藤杀898

sudo在终端窗口中杀死898“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

如果一切顺利,该过程将以静默方式终止。

在终端窗口中从sudo kill 898输出“ width =” 646“ height =” 77“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this); ;

相关:如何从Linux终端中终止进程

按名称杀死进程

使用pkill命令可以按名称杀死进程。确保您确定了正确的过程此命令将终止顶层进程。

sudo pkill上衣

终端窗口中的sudo pkill顶部“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

同样,没有消息是好消息。该过程无提示地终止。

终端窗口中sudo pkill top的输出“ width =” 646“ height =” 77“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this); ;

按名称杀死多个进程

如果您正在运行一个进程的多个副本,或者一个进程产生了多个子进程(例如Google Chrome浏览器可以做到),那么如何杀死它们?就这么简单。我们使用killall命令。

我们有两本表现最佳的专辑:

ps -e | grep top

ps -e | grep在终端窗口中的顶部“ width =” 646“ height =” 122“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);”

我们可以使用以下命令终止它们:

sudo killall上衣

sudo killall在终端窗口中的顶部“ width =” 646“ height =” 57“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

没有响应就意味着没有问题,因此这两个过程都已终止。

在终端窗口中从sudo killall顶部输出“ width =” 646“ height =” 77“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this); ;

使用顶部获取动态视图

ps的输出是快照视图。它不会更新。要获取进程的更新视图,请使用top命令。它提供了计算机中正在运行的进程的动态视图。显示分为两部分。屏幕顶部有一个仪表板区域,由文本行组成,屏幕下部有一个表,由列组成。

从此命令开始:

最佳

在终端窗口中运行的顶部命令“ width =” 646“ height =” 382“ onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);“ onerror =” this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);

这些列包含有关流程的信息:

  • PID:进程ID
  • USER:流程所有者的名称
  • PR:流程优先
  • NI:这个过程的价值
  • VIRT:进程使用的虚拟内存
  • RES:进程使用的常驻内存
  • SHR:进程使用的共享内存
  • S:进程状态。请参阅以下此字段可以采用的值的列表
  • %CPU:自上次更新以来该进程使用的CPU时间的份额
  • %MEM:使用的物理内存份额
  • TIME +:任务使用的总CPU时间,以百分之一秒为单位
  • COMMAND:命令名称或命令行(名称和命令行参数)如果看不到命令​​列,请按“向右箭头”键。

进程的状态可以是以下之一:

  • D:不间断的睡眠
  • R:跑步
  • S:睡觉
  • T:已追踪(已停止)
  • Z:僵尸

按“ Q”键从顶部退出。

相关:您应该知道的37个重要的Linux命令

在终止进程之前

确保它是您所追求的,并确保它不会给您造成任何问题。特别值得一提的是,请使用-H(层次结构)和–forest选项进行检查,以确保它没有遗忘的重要子进程。

资讯来源:由0x资讯编译自HOWTOGEEK。版权归作者Dave McKay所有,未经许可,不得转载
提示:投资有风险,入市需谨慎,本资讯不作为投资理财建议。请理性投资,切实提高风险防范意识;如有发现的违法犯罪线索,可积极向有关部门举报反映。
你可能还喜欢