linux下ps命令

Linux中的ps命令是Process Status的缩写。ps命令用来列出系统中当前运行的那些进程。ps命令列出的是当前那些进程的快照,

就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命令

要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而 ps 命令就是最基本同时也是非常强大的进程查看命令。使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等。总之大部分信息都是可以通过执行该命令得到的。

命令参数: a 显示所有进程 -a 显示同一终端下的所有程序 -A 显示所有进程 c 显示进程的真实名称 -N 反向选择 -e 等于“-A” e 显示环境变量 f 显示程序间的关系 -H 显示树状结构 r 显示当前终端的进程 T 显示当前终端的所有程序 u 指定用户的所有进程 -au 显示较详细的资讯 -aux 显示所有包含其他使用者的行程 -C<命令> 列出指定命令的状况 --lines<行数> 每页显示的行数 --width<字符数> 每页显示的字符数 --help 显示帮助信息 --version 显示版本显示

 

1. pstree

pstree以树结构显示进程
$ pstree -p work | grep ad

sshd(22669)---bash(22670)---ad_preprocess(4551)-+-{ad_preprocess}(4552) |-{ad_preprocess}(4553) |-{ad_preprocess}(4554) |-{ad_preprocess}(4555) |-{ad_preprocess}(4556) `-{ad_preprocess}(4557)

 

work为工作用户,-p为显示进程识别码,ad_preprocess共启动了6个子线程,加上主线程共7个线程

2. ps -Lf

$ ps -Lf 4551

UID PID PPID LWP C NLWP STIME TTY STAT TIME CMD work 4551 22670 4551 2 7 16:30 pts/2 Sl+ 0:02 ./ad_preprocess work 4551 22670 4552 0 7 16:30 pts/2 Sl+ 0:00 ./ad_preprocess work 4551 22670 4553 0 7 16:30 pts/2 Sl+ 0:00 ./ad_preprocess work 4551 22670 4554 0 7 16:30 pts/2 Sl+ 0:00 ./ad_preprocess work 4551 22670 4555 0 7 16:30 pts/2 Sl+ 0:00 ./ad_preprocess work 4551 22670 4556 0 7 16:30 pts/2 Sl+ 0:00 ./ad_preprocess work 4551 22670 4557 0 7 16:30 pts/2 Sl+ 0:00 ./ad_preprocess

进程共启动了7个线程

 

linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生) 4. 僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放) 5. 停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行) ps工具标识进程的5种状态码: D 不可中断 uninterruptible sleep (usually IO) R 运行 runnable (on run queue) S 中断 sleeping T 停止 traced or stopped Z 僵死 a defunct (”zombie”) process

 

3. pstack

pstack显示每个进程的栈跟踪

linux下ps命令linux下ps命令

$ pstack 4551 Thread 7 (Thread 1084229984 (LWP 4552)): #0 0x000000302afc63dc in epoll_wait () from /lib64/tls/libc.so.6 #1 0x00000000006f0730 in ub::EPollEx::poll () #2 0x00000000006f172a in ub::NetReactor::callback () #3 0x00000000006fbbbb in ub::UBTask::CALLBACK () #4 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0 #5 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6 #6 0x0000000000000000 in ?? () Thread 6 (Thread 1094719840 (LWP 4553)): #0 0x000000302afc63dc in epoll_wait () from /lib64/tls/libc.so.6 #1 0x00000000006f0730 in ub::EPollEx::poll () #2 0x00000000006f172a in ub::NetReactor::callback () #3 0x00000000006fbbbb in ub::UBTask::CALLBACK () #4 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0 #5 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6 #6 0x0000000000000000 in ?? () Thread 5 (Thread 1105209696 (LWP 4554)): #0 0x000000302b80baa5 in __nanosleep_nocancel () #1 0x000000000079e758 in comcm::ms_sleep () #2 0x00000000006c8581 in ub::UbClientManager::healthyCheck () #3 0x00000000006c8471 in ub::UbClientManager::start_healthy_check () #4 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0 #5 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6 #6 0x0000000000000000 in ?? () Thread 4 (Thread 1115699552 (LWP 4555)): #0 0x000000302b80baa5 in __nanosleep_nocancel () #1 0x0000000000482b0e in armor::armor_check_thread () #2 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0 #3 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6 #4 0x0000000000000000 in ?? () Thread 3 (Thread 1126189408 (LWP 4556)): #0 0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6 #1 0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6 #2 0x000000000044c972 in Business_config_manager::run () #3 0x0000000000457b83 in Thread::run_thread () #4 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0 #5 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6 #6 0x0000000000000000 in ?? () Thread 2 (Thread 1136679264 (LWP 4557)): #0 0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6 #1 0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6 #2 0x00000000004524bb in Process_thread::sleep_period () #3 0x0000000000452641 in Process_thread::run () #4 0x0000000000457b83 in Thread::run_thread () #5 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0 #6 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6 #7 0x0000000000000000 in ?? () Thread 1 (Thread 182894129792 (LWP 4551)): #0 0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6 #1 0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6 #2 0x0000000000420d79 in Ad_preprocess::run () #3 0x0000000000450ad0 in main ()

View Code

 

4、kill 终止进程 

有十几种控制进程的方法,下面是一些常用的方法: kill -STOP [pid] 发送SIGSTOP (17,19,23)停止一个进程,而并不消灭这个进程。 kill -CONT [pid] 发送SIGCONT (19,18,25)重新开始一个停止的进程。 kill -KILL [pid] 发送SIGKILL (9)强迫进程立即停止,并且不实施清理操作。 kill -9 -1 终止你拥有的全部进程。 SIGKILL 和 SIGSTOP 信号不能被捕捉、封锁或者忽略,但是,其它的信号可以。所以这是你的终极武器。

 

显示所有进程信息

命令:

ps -A

输出:

 PID TTY TIME CMD 1 ? 00:00:08 systemd 2 ? 00:00:00 kthreadd 4 ? 00:00:00 kworker/0:0H 6 ? 00:00:00 mm_percpu_wq 7 ? 00:00:04 ksoftirqd/0 8 ? 00:00:03 rcu_sched 9 ? 00:00:00 rcu_bh 10 ? 00:00:00 migration/0 11 ? 00:00:00 watchdog/0 12 ? 00:00:00 cpuhp/0 13 ? 00:00:00 kdevtmpfs 。。。省略

显示指定用户信息

命令:

ps -u root

输出:

 PID TTY TIME CMD 1 ? 00:00:08 systemd 2 ? 00:00:00 kthreadd 4 ? 00:00:00 kworker/0:0H 6 ? 00:00:00 mm_percpu_wq 7 ? 00:00:04 ksoftirqd/0 8 ? 00:00:03 rcu_sched 9 ? 00:00:00 rcu_bh 10 ? 00:00:00 migration/0 11 ? 00:00:00 watchdog/0 12 ? 00:00:00 cpuhp/0 13 ? 00:00:00 kdevtmpfs 14 ? 00:00:00 netns 15 ? 00:00:00 rcu_tasks_kthre

显示所有进程信息,连同命令行

命令:

ps -ef

输出:

UID PID PPID C STIME TTY TIME CMD root 1 0 0 May23 ? 00:00:08 /lib/systemd/systemd --system --deserialize 21 root 2 0 0 May23 ? 00:00:00 [kthreadd] root 4 2 0 May23 ? 00:00:00 [kworker/0:0H] root 6 2 0 May23 ? 00:00:00 [mm_percpu_wq] root 7 2 0 May23 ? 00:00:04 [ksoftirqd/0] root 8 2 0 May23 ? 00:00:03 [rcu_sched] root 9 2 0 May23 ? 00:00:00 [rcu_bh] root 10 2 0 May23 ? 00:00:00 [migration/0] root 11 2 0 May23 ? 00:00:00 [watchdog/0] root 12 2 0 May23 ? 00:00:00 [cpuhp/0] root 13 2 0 May23 ? 00:00:00 [kdevtmpfs] root 14 2 0 May23 ? 00:00:00 [netns] root 15 2 0 May23 ? 00:00:00 [rcu_tasks_kthre] root 16 2 0 May23 ? 00:00:00 [kauditd] root 17 2 0 May23 ? 00:00:00 [khungtaskd] root 18 2 0 May23 ? 00:00:00 [oom_reaper] 。。。

实例4: ps 与grep 常用组合用法,查找特定进程

命令:

ps -ef|grep ssh

输出:

gec@ubuntu:~/myshare$ ps -ef|grep bash gec 9448 79650 0 06:05 pts/4 00:00:00 grep --color=auto bash gec 79650 79575 0 03:49 pts/4 00:00:00 bash gec@ubuntu:~/myshare$ ps -ef | grep ssh gec 9452 79650 0 06:05 pts/4 00:00:00 grep --color=auto ssh root 16628 1 0 03:45 ? 00:00:00 /usr/sbin/sshd -D

将目前属于您自己这次登入的 PID 与相关信息列示出来

命令:

ps -l

输出:

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 R 1000 9455 79650 0 80 0 - 7657 - pts/4 00:00:00 ps 0 S 1000 79650 79575 0 80 0 - 6093 wait pts/4 00:00:00 bash

说明:

各相关信息的意义:

F 代表这个程序的旗标 (flag), 4 代表使用者为 super user

S 代表这个程序的状态 (STAT),关于各 STAT 的意义将在内文介绍

UID 程序被该 UID 所拥有

PID 就是这个程序的 ID !

PPID 则是其上级父程序的ID

C CPU 使用的资源百分比

PRI 这个是 Priority (优先执行序) 的缩写,详细后面介绍

NI 这个是 Nice 值,在下一小节我们会持续介绍

ADDR 这个是 kernel function,指出该程序在内存的那个部分。如果是个 running的程序,一般就是 “-“

SZ 使用掉的内存大小

WCHAN 目前这个程序是否正在运作当中,若为 – 表示正在运作

TTY 登入者的终端机位置

TIME 使用掉的 CPU 时间。

CMD 所下达的指令为何

在预设的情况下, ps 仅会列出与目前所在的 bash shell 有关的 PID 而已,所以, 当我使用 ps -l 的时候,只有三个 PID。

 

列出目前所有的正在内存当中的程序

命令:

ps aux

输出:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.3 185228 3100 ? Ss May23 0:08 /lib/systemd/systemd --system --deserialize 21 root 2 0.0 0.0 0 0 ? S May23 0:00 [kthreadd] root 4 0.0 0.0 0 0 ? I< May23 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? I< May23 0:00 [mm_percpu_wq] root 7 0.0 0.0 0 0 ? S May23 0:04 [ksoftirqd/0] root 8 0.0 0.0 0 0 ? I May23 0:03 [rcu_sched] root 9 0.0 0.0 0 0 ? I May23 0:00 [rcu_bh] root 10 0.0 0.0 0 0 ? S May23 0:00 [migration/0] root 11 0.0 0.0 0 0 ? S May23 0:00 [watchdog/0]

 

USER:该 process 属于那个使用者账号的 PID :该 process 的号码 %CPU:该 process 使用掉的 CPU 资源百分比 %MEM:该 process 所占用的物理内存百分比 VSZ :该 process 使用掉的虚拟内存量 (Kbytes) RSS :该 process 占用的固定的内存量 (Kbytes) TTY :该 process 是在那个终端机上面运作,若与终端机无关,则显示 ?,另外, tty1-tty6 是本机上面的登入者程序,若为 pts/0 等等的,则表示为由网络连接进主机的程序。 STAT:该程序目前的状态,主要的状态有 R :该程序目前正在运作,或者是可被运作 S :该程序目前正在睡眠当中 (可说是 idle 状态),但可被某些讯号 (signal) 唤醒。 T :该程序目前正在侦测或者是停止了 Z :该程序应该已经终止,但是其父程序却无法正常的终止他,造成 zombie (疆尸) 程序的状态 START:该 process 被触发启动的时间 TIME :该 process 实际使用 CPU 运作的时间 COMMAND:该程序的实际指令

 

列出类似程序树的程序显示

命令:

ps -axjf

输出:

 PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 0 2 0 0 ? -1 S 0 0:00 [kthreadd] 2 4 0 0 ? -1 I< 0 0:00 _ [kworker/0:0H] 2 6 0 0 ? -1 I< 0 0:00 _ [mm_percpu_wq] 2 7 0 0 ? -1 S 0 0:04 _ [ksoftirqd/0] 2 8 0 0 ? -1 R 0 0:03 _ [rcu_sched] 2 9 0 0 ? -1 I 0 0:00 _ [rcu_bh] 2 10 0 0 ? -1 S 0 0:00 _ [migration/0] 2 11 0 0 ? -1 S 0 0:00 _ [watchdog/0] 2 12 0 0 ? -1 S 0 0:00 _ [cpuhp/0] 2 13 0 0 ? -1 S 0 0:00 _ [kdevtmpfs] 2 14 0 0 ? -1 I< 0 0:00 _ [netns] 2 15 0 0 ? -1 S 0 0:00 _ [rcu_tasks_kthre] 2 16 0 0 ? -1 S 0 0:00 _ [kauditd]

找出与 cron 与 syslog 这两个服务有关的 PID 号码

命令:ps aux | egrep ‘(cron|syslog)’

root 786 0.0 0.0 30748 508 ? Ss May23 0:00 /usr/sbin/cron -f syslog 788 0.0 0.0 256392 208 ? Ssl May23 0:00 /usr/sbin/rsyslogd -n gec 2147 0.0 0.0 436552 672 ? S<l May23 0:00 /usr/bin/pulseaudio --start --log-target=syslog gec 9483 0.0 0.1 15964 1032 pts/4 S+ 06:13 0:00 grep -E --color=auto (cron|syslog)

说明:

 

其他实例:

1. 可以用 | 管道和 more 连接起来分页查看

命令:

ps -aux |more

2. 把所有进程显示出来,并输出到ps001.txt文件

命令:

ps -aux > ps001.txt

3. 输出指定的字段

命令:

 ps -o pid,ppid,pgrp,session,tpgid,comm

输出:

 

原文链接:https://www.cnblogs.com/xiangtingshen/p/10920236.html

原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/33187

(0)
上一篇 2024年8月31日 01:58
下一篇 2024年8月31日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

优速盾注册领取大礼包www.cdnb.net
/sitemap.xml