[实践OK]CentOS使用下ps命令无头--no-headers下计算PHP-FPM进程内存统计的方法,查看PPID以及PHP启动守护进程的进程名执行后请用ps查看是否存在两个ppid为1的主进程,如果存在,使用kill杀掉重启中未正常杀掉的进程 PHP启动的守护进程:ps -f -C php  PHP-FPM进程:ps -f -C php,Centos7安装pstree之yum install psmisc,Linux下多线程查看工具(pstree、ps、pstack),linux命令之-pstree使用说明, linux/centos  man中有ps -eLf查看线程状态。

jackxiang 2015-1-18 21:23 | |
man ps里有:
ps -eLf查看线程状态,获取线程信息
-e     Select all processes.  Identical to -A.  -e 选择所有进程。 与 -A 相同。
ps -eLf
       -f 做全格式列表。该选项可与许多其他 UNIX 样式的选项结合使用,以添加额外的列。 它还会导致参数打印出来。 与 -L 一起使用时,将添加 NLWP(线程数)和 LWP(线程 ID)列。 参见 c 选项、格式关键字 args 和格式关键字 comm。


不加-e的全量,加上进程名查看此进程的所有线程(LWP (thread ID)):
ps -Lf -C pdfwatcherd
UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
root     48369     1 48369  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48370  0    8 Dec08 ?        00:00:01 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48371  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48372  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48373  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48375  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48376  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48377  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48374  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48378  0    6 Dec08 ?        00:00:01 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48379  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48380  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48381  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48382  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d

ps -ef|grep pdfwatcherd
root     37734  7504  0 11:31 pts/1    00:00:00 grep --color=auto pdfwatcherd
root     48369     1  0 Dec08 ?        00:00:01 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369  0 Dec08 ?        00:00:01 /usr/local/pdfwatcher/bin/pdfwatcherd -d
48374 下面有:5个线程(48374 48369 48374 除外):
root     48374 48369 48378  0    6 Dec08 ?        00:00:01 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48379  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48380  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48381  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48374 48369 48382  0    6 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d

48369(48369     1 )下面有7个线程(去掉root     48369     1 48369):
root     48369     1 48370  0    8 Dec08 ?        00:00:01 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48371  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48372  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48373  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48375  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48376  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d
root     48369     1 48377  0    8 Dec08 ?        00:00:00 /usr/local/pdfwatcher/bin/pdfwatcherd -d


ps axms
ps -T -p <pdfwatcherd的PID>
将列出该进程所有线程的信息,包括线程的 ID(TID)、状态、运行时间等。
ps -T -p 48369
  PID  SPID TTY          TIME CMD
48369 48369 ?        00:00:00 pdfwatcherd
48369 48370 ?        00:00:01 pdfwatcherd
48369 48371 ?        00:00:00 pdfwatcherd
48369 48372 ?        00:00:00 pdfwatcherd
48369 48373 ?        00:00:00 pdfwatcherd
48369 48375 ?        00:00:00 pdfwatcherd
48369 48376 ?        00:00:00 pdfwatcherd
48369 48377 ?        00:00:00 pdfwatcherd
-T     Show threads, possibly with SPID column.
-p pidlist
               Select by PID.  This selects the processes whose process ID numbers appear in pidlist.  Identical to p and --pid.

打印进程树
ps -ejH
ps axjf

CentOS使用下ps命令无头计算PHP-FPM进程内存统计的方法:
0)100个子进程,加上主进程101
1)每个PHP不到10M
2)Ps显示的是kb,得大体除以1024


--no-headers    print no header line at all
ps -f -C  php --no-headers
ps -f -C  php --no-headers


看Logstash多线程, L的意思,light weight process:


执行后请用ps查看是否存在两个ppid为1的主进程,如果存在,使用kill杀掉重启中未正常杀掉的进程
PHP启动的守护进程:ps -f -C php
PHP-FPM进程:ps -f -C php-fpm

PHP-FPM的单进程单线程模型,用ps 命令,-L参数显示进程,并尽量显示其LWP(线程ID)和NLWP(线程的个数),如下:
[root@rh08 none]# ps -eLf | grep php-fpm
www      39154 22867 39154  2    1 10:38 ?        00:00:01 php-fpm: pool www                                                                                                            
www      39155 22867 39155  2    1 10:38 ?        00:00:01 php-fpm: pool www                                                                                                            
www      39156 22867 39156  2    1 10:38 ?        00:00:01 php-fpm: pool www                                                                                                            
www      39157 22867 39157  2    1 10:38 ?        00:00:01 php-fpm: pool www                                                                                                            
www      39158 22867 39158  2    1 10:38 ?        00:00:01 php-fpm: pool www
上面命令查询结果的第二列为PID,第三列为PPID,第四列为LWP,第六列为NLWP。

ps命令还可以查看线程在哪个CPU上运行:
[root@rh08 none]# ps -eo ruser,pid,ppid,lwp,psr,args -L | grep php
root     22867     1 22867   1 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)                                                                    
www      38929 22867 38929   0 php-fpm: pool www                                                                                                            
www      38930 22867 38930   0 php-fpm: pool www                                                                                                            
www      38934 22867 38934   0 php-fpm: pool www                                                                                                            
www      38935 22867 38935   0 php-fpm: pool www                                                                                                            
www      38936 22867 38936   0 php-fpm: pool www                                                                                                            
www      38937 22867 38937   1 php-fpm: pool www    
From:http://smilejay.com/2012/06/linux_view_threads/

yum install epel-release -y
yum install htop -y

#pstree -c|grep shutdown
        |      |-sshd---sshd---bash---sudo---su---bash---go-+-shutdown-+-{shutdown}
        |      |                                            |          |-{shutdown}
        |      |                                            |          |-{shutdown}
        |      |                                            |          |-{shutdown}
        |      |                                            |          `-{shutdown}

ps -T -p  12264
    PID    SPID TTY          TIME CMD
  12264   12264 pts/1    00:00:00 shutdown
  12264   12266 pts/1    00:00:00 shutdown
  12264   12267 pts/1    00:00:00 shutdown
  12264   12268 pts/1    00:00:00 shutdown
  12264   12269 pts/1    00:00:00 shutdown
  12264   12270 pts/1    00:00:00 shutdown

top -H -p  12264
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                            
  12264 root      20   0  702872   4924    740 S   0.0   0.6   0:00.00 shutdown                                                                          
  12266 root      20   0  702872   4924    740 S   0.0   0.6   0:00.00 shutdown                                                                          
  12267 root      20   0  702872   4924    740 S   0.0   0.6   0:00.00 shutdown                                                                          
  12268 root      20   0  702872   4924    740 S   0.0   0.6   0:00.00 shutdown                                                                          
  12269 root      20   0  702872   4924    740 S   0.0   0.6   0:00.00 shutdown                                                                          
  12270 root      20   0  702872   4924    740 S   0.0   0.6   0:00.00 shutdown

Htop:
yum install htop  

htop -p 67517
    PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command                                                                                     67519 root       20   0  686M  2880   740 S  0.0  0.3  0:00.00 /tmp/go-build3856511210/b001/exe/shutdown                                                



  67520 root       20   0  686M  2880   740 S  0.0  0.3  0:00.00 /tmp/go-build3856511210/b001/exe/shutdown
  67521 root       20   0  686M  2880   740 S  0.0  0.3  0:00.00 /tmp/go-build3856511210/b001/exe/shutdown
  67522 root       20   0  686M  2880   740 S  0.0  0.3  0:00.00 /tmp/go-build3856511210/b001/exe/shutdown

Display Options ,[V]Tree view  [V]show custom thread name ,勾选是空格键盘,上下键移动菜单条目,OK后,按<F10>退出设置,左斜杠搜索F2也是搜索,F3是Next,ESC推出。
一个对用户更加友好的方式是,通过htop查看单个进程的线程,它是一个基于ncurses的交互进程查看器。该程序允许你在树状视图中监控单个独立线程。
要在htop中启用线程查看,请开启htop,然后按<F2>来进入htop的设置菜单。选择“设置”栏下面的“显示选项”,然后开启“树状视图”和“显示自定义线程名”选项。按<F10>退出设置。
From: http://www.codeceo.com/article/linux-find-thread.html

0.最常用 pstree:
[root@iZ25dcp92ckZ temp]# pstree -a|grep multe
  |       |   `-multepoolser
  |       |       `-multepoolser
  |       |           `-2*[{multepoolser}]

1. > top

可以显示所有系统进程

按u, 再输入相应的执行用户名称,比如Tom

可以看到Tom用户启动的所有进程和对应的pid

2. > pstack pid

可以看到此pid下,各线程的运行状态、

[root@test multepoolserver]# pstack  14944  (进程的PID号)
Thread 2 (Thread 0x41ed5940 (LWP 14945)):
#0  0x0000003c9ae0d5cb in read () from /lib64/libpthread.so.0
#1  0x00000000004017b6 in sync_additional_writing_worker ()
#2  0x0000003c9ae064a7 in start_thread () from /lib64/libpthread.so.0
#3  0x0000003c9a2d3c2d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x2b24b3094250 (LWP 14944)):
#0  0x0000003c9a2d4018 in epoll_wait () from /lib64/libc.so.6
#1  0x0000000000401d59 in Process ()
#2  0x00000000004029b8 in main ()

来自:http://blog.csdn.net/wind_324/article/details/6152912

方法一:
ps -ef f
用树形显示进程和线程
在Linux下面好像因为没有真正的线程,是用进程模拟的,有一个是辅助线程,所以真正程序开的线程应该只有一个。

方法二:
[root@apache dhj]# ps axm|grep httpd

方法三:
另外用pstree -c也可以达到相同的效果,但是没有线程号:
[root@apache dhj]# pstree -c|grep httpd

来自:http://blog.chinaunix.net/uid-346158-id-2131012.html

1. pstree

pstree以树结构显示进程


2. ps -Lf
$ ps -Lf 1892
如下:PHP进程共启动了0个线程
root@119.10.6.**:~# ps -Lf 1892
UID        PID  PPID   LWP  C NLWP STIME TTY      STAT   TIME CMD
root      1892     1  1892  0    1 Jan15 ?        Ss     0:53 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)

Mysql多个线程:
root@119.10.6.*:~#  ps -Lf 3005
UID        PID  PPID   LWP  C NLWP STIME TTY      STAT   TIME CMD
mysql     3005  2011  3005  0   39 Jan15 ?        Sl     2:25 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=
mysql     3005  2011  3081  0   39 Jan15 ?        Sl     0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=
mysql     3005  2011  3082  0   39 Jan15 ?        Sl     0:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=
mysql     3005  2011  3083  0   39 Jan15 ?        Sl     0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=
mysql     3005  2011  3084  0   39 Jan15 ?        Sl     0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=
mysql     3005  2011  3085  0   39 Jan15 ?        Sl     0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=
mysql     3005  2011  3086  0   39 Jan15 ?        Sl     0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=
mysql     3005  2011  3087  0   39 Jan15 ?        Sl     0:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=

3. pstack
pstack显示每个进程的栈跟踪,PHP的fpm主进程,发现这玩意是走的epoll:
root@119.10.6.23:~# pstack 1892
#0  0x00000030e9ae8fb3 in __epoll_wait_nocancel () from /lib64/libc.so.6
#1  0x0000000000856a74 in fpm_event_epoll_wait ()
#2  0x000000000084afff in fpm_event_loop ()
#3  0x0000000000845ee7 in fpm_run ()
#4  0x000000000084d900 in main ()
You have new mail in /var/spool/mail/root

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/7737/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2023-12-19 11:34
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]