标签归档:进程

Linux 查看进程

杀掉登录用户:

w
 20:46:40 up 12 days,  4:28,  4 users,  load average: 0.30, 0.12, 0.07
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    112.97.63.53     20:28    0.00s  0.07s  0.00s w
root     pts/2    125.94.151.83    14:08    3:31m  0.04s  0.04s -bash

ps -ef | grep pts/2

#向上寻找父进程ID
kill -9 pid

动态查看进程CPU内存

#-d 1表示1秒刷新
top -d 1 -p pid

这个可以查看百分比,比如内存占用,如果要查看进程实际霸占的内存,可以通过ps命令。

查看进程霸占内存:

ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

RSS对应实际霸占的内存(KB),这里需要知道,进程占用内存一般是指堆内存(其它一般占比较少),而堆内存内中是有空闲的,不过这个都归到进程内存占用。另外,如果要查看进程更加详细的信息:

cat /proc/pid/status

Name:	node
State:	S (sleeping)
Tgid:	31897
Ngid:	0
Pid:	31897    进程号
PPid:	1        父进程
TracerPid:	0
Uid:	0	0	0	0
Gid:	0	0	0	0
FDSize:	256
Groups:	0 
VmPeak:	 1302912 kB
VmSize:	 1301888 kB
VmLck:	       0 kB
VmPin:	       0 kB
VmHWM:	  123372 kB
VmRSS:	  122772 kB实际霸占内存
VmData:	 1262976 kB
VmStk:	     136 kB
VmExe:	   19980 kB
VmLib:	    4056 kB
VmPTE:	     636 kB
VmSwap:	       0 kB
Threads:	9  线程数量
SigQ:	0/31225
SigPnd:	0000000000000000
ShdPnd:	0000000000000000
SigBlk:	0000000000000000
SigIgn:	0000000000001000
SigCgt:	0000000188004202
CapInh:	0000000000000000
CapPrm:	0000001fffffffff
CapEff:	0000001fffffffff
CapBnd:	0000001fffffffff
Seccomp:	0
Cpus_allowed:	7fff
Cpus_allowed_list:	0-14
Mems_allowed:	00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:	0
voluntary_ctxt_switches:	1373
nonvoluntary_ctxt_switches:	4465

进程监控与管理 – supervisor

#预先安装epel源(supervisor依赖python-meld3,实际就是python写的脚本)
yum install supervisor

[root@vfeelit ~]# rpm -qa | grep supervisor
supervisor-3.1.3-3.el7.noarch

[root@vfeelit ~]# rpm -ql supervisor-3.1.3-3.el7.noarch
/etc/logrotate.d/supervisor  #滚动日志
/etc/supervisord.conf	     #配置文件
/etc/tmpfiles.d/supervisor.conf

/usr/bin/pidproxy
/usr/bin/supervisorctl       #进程控制
/usr/bin/supervisord 	     #进程

/usr/lib/python2.7/site-packages/supervisor/*
/usr/lib/python2.7/site-packages/supervisor/supervisorctl.py
/usr/lib/python2.7/site-packages/supervisor/supervisorctl.pyc
/usr/lib/python2.7/site-packages/supervisor/supervisorctl.pyo
/usr/lib/python2.7/site-packages/supervisor/supervisord.py
/usr/lib/python2.7/site-packages/supervisor/supervisord.pyc
/usr/lib/python2.7/site-packages/supervisor/supervisord.pyo

/usr/lib/systemd/system/supervisord.service  #服务脚本
/var/log/supervisor  # 日志
/var/run/supervisor  # PID

查看服务启动脚本/usr/lib/systemd/system/supervisord.service,可以看到主要运行
/usr/bin/supervisord -c /etc/supervisord.conf。

查看/usr/bin/supervisord:

[root@vfeelit ~]# cat /usr/bin/supervisord
#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'supervisor==3.1.3','console_scripts','supervisord'
__requires__ = 'supervisor==3.1.3'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('supervisor==3.1.3', 'console_scripts', 'supervisord')()
    )

使用/usr/bin/python解释器,靠,这个世界还有如此难看的代码。大概意思应该就是运行supervisord这个脚本吧,根据我的感觉,应该是/usr/lib/python2.7/site-packages/supervisor/supervisord.py。知道那么回事就可以了。这种工具主要看怎么配置,以及用法。

配置文件/etc/supervisord.conf中的配置实在太多,不过这个文件中的东西,看起来都不需要动它,这个配置语法是典型ini配置:

[supervisord]   //主进程的配置
[supervisorctl]
[include]
files = supervisord.d/*.ini

自己的配置,放入supervisord.d即可。

例子:

command=/var/www/server.php	;要运行的命令
autostart=true 			;自动启动
autorestart=true 		;自动重启
redirect_stderr=true 		;将stderr重定向到stdout
user=www			;以哪个用户启动命令
directory=/var/www	 	;cd 到应用目录
stdout_logfile = /var/www/x.log ;日志输出

[program:xxx-worker]
process_name=%(program_name)s_%(process_num)02d
command=php artisan queue:work --queue=parser --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=www
numprocs=16			;进程数量
redirect_stderr=true
stdout_logfile=/var/log/worker.log

命令的运行,一般写入绝对路径比较稳妥。user指定了运行脚本的用户(要注意权限问题)。numprocs指定要保持的进程数量,这个参数如果大于1,就需要设置process_name(参考例子),因为它需要一个唯一的名字。另外一个是日志输出,应该是进程相关,或命令行运行产生的错误相关的。

说明:

%(program_name)s	表示program_name是一个字符串
%(process_num)02d	表示process_num是数字(d),并且是两位数字,不足用0填充

客户端操作:

[root@vfeelit ~]# supervisorctl 
test-worker:test-worker_00       RUNNING   pid 2834, uptime 0:01:03
test-worker:test-worker_01       RUNNING   pid 2835, uptime 0:01:03
test-worker:test-worker_02       RUNNING   pid 2836, uptime 0:01:03
test-worker:test-worker_03       RUNNING   pid 2837, uptime 0:01:02
test-worker:test-worker_04       RUNNING   pid 2838, uptime 0:01:01
supervisor> status
test-worker:test-worker_00       RUNNING   pid 2834, uptime 0:01:10
test-worker:test-worker_01       RUNNING   pid 2835, uptime 0:01:10
test-worker:test-worker_02       RUNNING   pid 2836, uptime 0:01:10
test-worker:test-worker_03       RUNNING   pid 2837, uptime 0:01:09
test-worker:test-worker_04       RUNNING   pid 2838, uptime 0:01:08
supervisor> help

default commands (type help <topic>):
=====================================
add    clear  fg        open  quit    remove  restart   start   stop  update 
avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version

supervisor> stop help
help: ERROR (no such process)
supervisor> stop
Error: stop requires a process name
stop <name>		Stop a process
stop <gname>:*		Stop all processes in a group
stop <name> <name>	Stop multiple processes or groups
stop all		Stop all processes
supervisor> version
3.1.3
supervisor> pid
2757
supervisor> restart 
Error: restart requires a process name
restart <name>		Restart a process
restart <gname>:*	Restart all processes in a group
restart <name> <name>	Restart multiple processes or groups
restart all		Restart all processes
Note: restart does not reread config files. For that, see reread and update.

Supervisor可以按组进行配置。

最后查看一下进程树:

└─system.slice
├─supervisord.service
│ ├─2757 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
│ ├─2834 php /root/worker.php
│ ├─2835 php /root/worker.php
│ ├─2836 php /root/worker.php
│ ├─2837 php /root/worker.php
│ └─2838 php /root/worker.php
[/shll]
从supervisord派生子进程,意思是它可控制的。也就意味如果不是它派生的,是无法监控的。