linux ps命令
Linux中的ps命令是Process Status的缩写。ps命令用来列出系统中当前运行的那些进程。ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命令。
要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而 ps 命令就是最基本同时也是非常强大的进程查看命令。使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等。总之大部分信息都是可以通过执行该命令得到的。
ps 为我们提供了进程的一次性的查看,它所提供的查看结果并不动态连续的;如果想对进程时间监控,应该用 top 工具。
kill 命令用于杀死进程。
linux上进程有5种状态:
运行(正在运行或在运行队列中等待)
中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)
不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生)
僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放)
停止(进程收到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
1.命令格式:
ps[参数]
2.命令功能:
用来显示当前进程的状态
3.命令参数:
a 显示所有进程
-a 显示同一终端下的所有程序
-A 显示所有进程
c 显示进程的真实名称
-N 反向选择
-e 等于“-A”
e 显示环境变量
f 显示程序间的关系
-H 显示树状结构
r 显示当前终端的进程
T 显示当前终端的所有程序
u 指定用户的所有进程
-au 显示较详细的资讯
-aux 显示所有包含其他使用者的行程
-C<命令> 列出指定命令的状况
--lines<行数> 每页显示的行数
--width<字符数> 每页显示的字符数
--help 显示帮助信息
--version 显示版本显示
4.使用实例:
实例1:显示所有进程信息
命令:
ps -A
输出:
[test@23-37-00 test]$ ps -A
PID TTY TIME CMD
1 ? 00:01:47 init
2 ? 00:00:00 kthreadd
3 ? 00:06:01 migration/0
4 ? 00:02:58 ksoftirqd/0
5 ? 00:00:00 stopper/0
6 ? 00:01:43 watchdog/0
7 ? 00:03:41 migration/1
8 ? 00:00:00 stopper/1
9 ? 00:01:43 ksoftirqd/1
10 ? 00:00:58 watchdog/1
……省略部分结果
实例2:显示指定用户信息
命令:
ps -u root
输出:
[test@28-4-28 test]$ ps -u work
PID TTY TIME CMD
6178 ? 00:02:23 python
9257 pts/12 00:00:00 vim
19362 ? 00:00:00 sshd
19368 pts/8 00:00:00 bash
20006 ? 00:00:01 sshd
20007 pts/4 00:00:00 bash
20164 ? 00:00:00 sshd
20165 pts/2 00:00:00 bash
20989 ? 00:00:00 sshd
20990 pts/3 00:00:00 bash
37747 pts/16 00:00:05 python
38393 pts/4 00:00:00 vim
38850 pts/3 00:00:00 ps
39229 ? 00:00:00 sh
40648 ? 00:00:00 sshd
40649 pts/11 00:00:00 bash
41411 ? 00:00:00 sshd
41412 pts/16 00:00:00 bash
44952 ? 00:00:00 sshd
44953 pts/12 00:00:00 bash
52899 ? 00:00:00 sshd
52956 pts/7 00:00:00 bash
53916 pts/7 00:00:50 java
57915 ? 00:00:00 sh
实例3:显示所有进程信息,连同命令行
命令:
ps -ef
输出:
[test@33-4-65test]$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 2017 ? 00:01:47 /sbin/init
root 2 0 0 2017 ? 00:00:00 [kthreadd]
root 3 2 0 2017 ? 00:06:01 [migration/0]
root 4 2 0 2017 ? 00:02:58 [ksoftirqd/0]
root 5 2 0 2017 ? 00:00:00 [stopper/0]
root 6 2 0 2017 ? 00:01:43 [watchdog/0]
root 7 2 0 2017 ? 00:03:41 [migration/1]
root 8 2 0 2017 ? 00:00:00 [stopper/1]
root 9 2 0 2017 ? 00:01:43 [ksoftirqd/1]
root 10 2 0 2017 ? 00:00:58 [watchdog/1]
root 11 2 0 2017 ? 00:02:19 [migration/2]
root 12 2 0 2017 ? 00:00:00 [stopper/2]
root 13 2 0 2017 ? 00:01:07 [ksoftirqd/2]
root 14 2 0 2017 ? 00:02:21 [watchdog/2]
root 15 2 0 2017 ? 00:00:31 [migration/3]
……省略部分结果
实例4: ps 与grep 常用组合用法,查找特定进程
命令:
ps -ef|grep ssh
输出:
[work@33-4-67 test]$ ps -ef | grep ssh
root 4073 1 0 2017 ? 00:01:18 /usr/sbin/sshd
root 19349 4073 0 19:44 ? 00:00:00 sshd: work [priv]
work 19362 19349 0 19:44 ? 00:00:00 sshd: work@pts/8
root 19997 4073 0 10:08 ? 00:00:00 sshd: work [priv]
work 20006 19997 0 10:08 ? 00:00:01 sshd: work@pts/4
root 20159 4073 0 19:14 ? 00:00:00 sshd: work [priv]
work 20164 20159 0 19:14 ? 00:00:00 sshd: work@pts/2
root 20987 4073 0 19:14 ? 00:00:00 sshd: work [priv]
work 20989 20987 0 19:14 ? 00:00:00 sshd: work@pts/3
root 40640 4073 0 14:27 ? 00:00:00 sshd: work [priv]
work 40648 40640 0 14:27 ? 00:00:00 sshd: work@pts/11
root 41393 4073 0 16:47 ? 00:00:00 sshd: work [priv]
work 41411 41393 0 16:47 ? 00:00:00 sshd: work@pts/16
work 43530 20990 0 20:29 pts/3 00:00:00 grep ssh
root 44935 4073 0 14:29 ? 00:00:00 sshd: work [priv]
work 44952 44935 0 14:29 ? 00:00:00 sshd: work@pts/12
root 52893 4073 0 11:19 ? 00:00:00 sshd: work [priv]
work 52899 52893 0 11:19 ? 00:00:00 sshd: work@pts/7
参考:http://www.cnblogs.com/peida/archive/2012/12/19/2824418.html
还没有评论,来说两句吧...