对linux/unix运维人员,ps是再熟悉不过的命令了
这个命令可以帮助我们查看服务器上的进程信息,在问题排查,日常监控,例行维护,上线变更过程中都发挥了重大的作用。可谓是居家旅行,杀人越货之良品。
那像这样一款产品,世面上一般都卖到几千甚至上万块,可是今天我们公司促销大酬宾,只要998,超级命令带回家,走过路过不要错过,全部998,通通998。。。。。
言归正传,ps命令的参数共有两种风格,分别是
BSD风格:选项可以组合在一起,并且前面不带-连字符
ps aux
UNIX风格:选项可以组合在一起,并且参数前面必须带-连字符
ps -ef
GNU风格:参数前面带有两个--连字符
ps --user root
3种风格各有千秋,下面就以UNIX风格为例,简单介绍一些常用参数:
-e|-A:显示所有信息
-f:显示进程的所有信息,通常包含UID,PID ,PPID,C,STIME,TTY,TIME,CMD
-F:从名字上就能看出,比-f显示更多更多的信息
-p:显示指定pid的信息
-u:显示指定用户的信息
-L:显示线程信息
-o:自定义选项(一个神奇的选项,o(^▽^)o),允许你自定义ps的输出内容,使用-o选项
下面我们做一些简单的练习
显示所有用户的所有线程信息[[email protected] ~]# ps -eLf | headUID PID PPID LWP C NLWP STIME TTY TIME CMDroot 1 0 1 0 1 08:00 ? 00:00:01 init [3]root 2 1 2 0 1 08:00 ? 00:00:00 [migration/0]root 3 1 3 0 1 08:00 ? 00:00:00 [ksoftirqd/0]root 4 1 4 0 1 08:00 ? 00:00:02 [events/0]root 5 1 5 0 1 08:00 ? 00:00:00 [khelper]root 14 1 14 0 1 08:00 ? 00:00:00 [kthread]root 18 14 18 0 1 08:00 ? 00:00:00 [kblockd/0]root 19 14 19 0 1 08:00 ? 00:00:00 [kacpid]root 187 14 187 0 1 08:00 ? 00:00:00 [cqueue/0]
显示smmsp用户的所有信息 [[email protected] ~]# ps -f -u smmspUID PID PPID C STIME TTY TIME CMDsmmsp 4130 1 0 08:01 ? 00:00:00 sendmail: Queue [email protected]:00:00 for /var/spool/clientmqueue
显示进程的启动时间,进程号,cpu使用率,内存使用,线程数,执行命令(是不是碉堡了)针对你的需求,可以适当的增加和删减自定义参数,便于使用 [[email protected] ~]# ps -eo start_time,pid,pcpu,rss,nlwp,cmd | head START PID %CPU RSS NLWP CMD 08:00 1 0.0 692 1 init [3] 08:00 2 0.0 0 1 [migration/0] 08:00 3 0.0 0 1 [ksoftirqd/0] 08:00 4 0.0 0 1 [events/0] 08:00 5 0.0 0 1 [khelper] 08:00 14 0.0 0 1 [kthread] 08:00 18 0.0 0 1 [kblockd/0] 08:00 19 0.0 0 1 [kacpid] 08:00 187 0.0 0 1 [cqueue/0] 例如我们想查看某个进程启动的所有线程号,可以这样做[[email protected] ~]# ps -p 3956 -Lo pid,lwp PID LWP 3956 3956 3956 3957 3956 3958 3956 3961 3956 3964 如果只想知道某个进程启动的线程数,则是这样[[email protected] ~]# ps -p 3956 -o lwp LWP 3956
再来个实用的栗子ps ewwwww可以用来查看进程启动时的环境变量 比如你的应用出现乱码了,可以使用这个命令来确认启动时是否使用了正确的LANG
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
进程状态篇,未完待续。。。。。。。
PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process.
D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its parent.
For BSD formats and when the stat keyword is used, additional characters may be displayed:
< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
KEY LONG DESCRIPTION
c cmd simple name of executable
C pcpu cpu utilization
f flags flags as in long format F field
g pgrp process group ID
G tpgid controlling tty process group ID
j cutime cumulative user time
J cstime cumulative system time
k utime user time
m min_flt number of minor page faults
M maj_flt number of major page faults
n cmin_flt cumulative minor page faults
N cmaj_flt cumulative major page faults
o session session ID
p pid process ID
P ppid parent process ID
r rss resident set size
R resident resident pages
s size memory size in kilobytes
S share amount of shared pages
t tty the device number of the controlling tty
T start_time time process was started
U uid user ID number
u user user name
v vsize total VM size in kB
y priority kernel scheduling priority
--------------------------------------------------------------------------------------------------------------------------------------------------------------------