Linux进程查看及管理的工具:pstree, ps, pidof, pgrep, top, htop, glance, pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup
1、pstree命令:
pstree - display a tree of processes
[[email protected] ~]# pstree
init─┬─agetty
├─auditd───{auditd}
├─crond
├─dbus-daemon
├─master─┬─pickup
│ └─qmgr
├─6*[mingetty]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───pstree
├─udevd───2*[udevd]
└─xe-daemon───sleep
2、ps: process state
ps - report a snapshot of the current processes
Linux系统各进程的相关信息均保存在/proc/PID目录下的各文件中;
ps [OPTION]...
选项:支持两种风格
常用组合:aux
u: 以用户为中心组织进程状态信息显示
a: 与终端相关的进程;
x: 与终端无关的进程;
[[email protected] ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
VSZ: Virtual memory SiZe,虚拟内存集
RSS: ReSident Size, 常驻内存集
STAT:进程状态
R:running
S: interruptable sleeping
D: uninterruptable sleeping
T: stopped
Z: zombie
+: 前台进程
l: 多线程进程
N:低优先级进程
<: 高优先级进程
s: session leader
常用组合:-ef
-e: 显示所有进程
-f: 显示完整格式程序信息
[[email protected] ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug05 ? 00:00:00 /sbin/init
root 2 0 0 Aug05 ? 00:00:00 [kthreadd]
[[email protected] ~]# ps -ef | head
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug05 ? 00:00:00 /sbin/init
root 2 0 0 Aug05 ? 00:00:00 [kthreadd]
root 3 2 0 Aug05 ? 00:00:00 [migration/0]
root 4 2 0 Aug05 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 Aug05 ? 00:00:00 [stopper/0]
root 6 2 0 Aug05 ? 00:00:00 [watchdog/0]
root 7 2 0 Aug05 ? 00:00:00 [migration/1]
root 8 2 0 Aug05 ? 00:00:00 [stopper/1]
root 9 2 0 Aug05 ? 00:00:00 [ksoftirqd/1]
常用组合:-eFH
-F: 显示完整格式的进程信息
-H: 以进程层级格式显示进程相关信息
常用组合:-eo, axo
-eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,comm
axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
ni: nice值
pri: priority,优先级
psr: processor, CPU
rtprio: 实时优先级
[[email protected] ~]# ps aux | grep java
root 25617 0.0 0.0 103312 880 pts/0 S+ 19:54 0:00 grep --color=auto java
[[email protected] ~]# ps -ef | grep java
root 25656 24755 0 19:54 pts/0 00:00:00 grep --color=auto java
查看root用户下面的进程-U root
[[email protected] ~]# ps -U root
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 kthreadd
3 ? 00:00:00 migration/0
4 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 stopper/0
3、pgrep, pkill:
pgrep [options] pattern
pkill [options] pattern
-u uid: effective user
-U uid: real user
-t terminal: 与指定终端相关的进程
-l: 显示进程名
-a: 显示完整格式的进程名
-P pid: 显示其父进程为此处指定的进程的进程列表
[[email protected] ~]# pgrep -u root
1
2
3
4
5
6
[[email protected] ~]# pgrep -u root -l
1 init
2 kthreadd
3 migration/0
4 ksoftirqd/0
5 stopper/0
6 watchdog/0
7 migration/1
8 stopper/1
4、pidof:
根据进程名获取其PID;
[[email protected] ~]# pidof init
1
5、top:
有许多内置命令:
排序:
P:以占据的CPU百分比;
M:占据内存百分比;
T:累积占据CPU时长;
首部信息显示:
uptime信息:l命令
[[email protected] ~]# uptime
20:34:49 up 3 days, 22 min, 1 user, load average: 0.00, 0.00, 0.00
tasks及cpu信息:t命令
cpu分别显示:1 (数字)
memory信息:m命令
退出命令:q
修改刷新时间间隔:s
终止指定进程:k
选项:
-d #: 指定刷新时间间隔,默认为3秒;
-b: 以批次方式;
-n #: 显示多少批次;
[[email protected] ~]# top
top - 20:30:18 up 3 days, 18 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 90 total, 1 running, 89 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.2%st
Mem: 1015856k total, 211348k used, 804508k free, 103108k buffers
Swap: 1015804k total, 0k used, 1015804k free, 36216k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27240 root 20 0 15024 1292 996 R 0.7 0.1 0:00.10 top
1 root 20 0 19232 1516 1228 S 0.0 0.1 0:00.30 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.95 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.05 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
6、htop命令:
选项:
-d #: 指定延迟时间;
-u UserName: 仅显示指定用户的进程;
-s COLOMN: 以指定字段进行排序;
命令:
s: 跟踪选定进程的系统调用;
l: 显示选定进程打开的文件列表;
a:将选定的进程绑定至某指定CPU核心;
t: 显示进程树
注意:Fedora-EPEL源
Linux基础:
CPU: timeslice
Memory: 线性地址空间
I/O:
分时复用
进程查看工具:pstree, ps, pgrep, pidof, top, htop
Linux进程查看及管理的工具:pstree, ps, pidof, pgrep, top, htop, glances, pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup
7、vmstat命令:
vmstat [options] [delay [count]]
procs:
r:等待运行的进程的个数;
b:处于不可中断睡眠态的进程个数;(被阻塞的队列的长度);
memory:
swpd: 交换内存的使用总量;
free:空闲物理内存总量;
buffer:用于buffer的内存总量;
cache:用于cache的内存总量;
swap:
si:数据进入swap中的数据速率(kb/s)
so:数据离开swap中的数据速率(kb/s)
io:
bi:从块设备读入数据到系统的速率;(kb/s)
bo: 保存数据至块设备的速率;
system:
in: interrupts, 中断速率;
cs: context switch, 进程切换速率;
cpu:
us
sy
id
wa
st
选项:
-s: 显示内存的统计数据
[[email protected] ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 554400 103744 282736 0 0 1 2 12 6 0 0 100 0 0
#表示每2秒执行一次 总共执行5次
[[email protected] ~]# vmstat 2 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 554524 103752 282756 0 0 1 2 12 6 0 0 100 0 0
0 0 0 554524 103752 282756 0 0 0 0 20 10 0 0 100 0 0
0 0 0 554524 103752 282756 0 0 0 0 17 7 0 0 100 0 0
0 0 0 554524 103752 282756 0 0 0 0 20 8 0 0 100 0 0
0 0 0 554524 103752 282756 0 0 0 28 17 9 0 0 100 0 0
8、pmap命令:
pmap - report memory map of a process
pmap [options] pid [...]
-x: 显示详细格式的信息;
另外一种实现:
# cat /proc/PID/maps
[[email protected] ~]# pmap 1
1: /sbin/init
00007fb2dd7cb000 52K r-x-- /lib64/libnss_files-2.12.so
00007fb2dd7d8000 2044K ----- /lib64/libnss_files-2.12.so
00007fb2dd9d7000 4K r---- /lib64/libnss_files-2.12.so
00007fb2dd9d8000 4K rw--- /lib64/libnss_files-2.12.so
00007fb2dd9d9000 1576K r-x-- /lib64/libc-2.12.so
9、glances命令:
glances [-bdehmnrsvyz1] [-B bind] [-c server] [-C conffile] [-p port] [-P password] [--password] [-t refresh] [-f file] [-o output]
内建命令:
a Sort processes automatically l Show/hide logs
c Sort processes by CPU% b Bytes or bits for network I/O
m Sort processes by MEM% w Delete warning logs
p Sort processes by name x Delete warning and critical logs
i Sort processes by I/O rate 1 Global CPU or per-CPU stats
d Show/hide disk I/O stats h Show/hide this help screen
f Show/hide file system stats t View network I/O as combination
n Show/hide network stats u View cumulative network I/O
s Show/hide sensors stats q Quit (Esc and Ctrl-C also work)
y Show/hide hddtemp stats
常用选项:
-b: 以Byte为单位显示网卡数据速率;
-d: 关闭磁盘I/O模块;
-f /path/to/somefile: 设定输入文件位置;
-o {HTML|CSV}:输出格式;
-m: 禁用mount模块
-n: 禁用网络模块
-t #: 延迟时间间隔
-1:每个CPU的相关数据单独显示;
C/S模式下运行glances命令:
服务模式:
glances -s -B IPADDR
IPADDR: 指明监听于本机哪个地址
客户端模式:
glances -c IPADDR
IPADDR:要连入的服务器端地址
10、dstat命令:
dstat [-afv] [options..] [delay [count]]
-c: 显示cpu相关信息;
-C #,#,...,total
-d: 显示disk相关信息;
-D total,sda,sdb,...
-g:显示page相关统计数据;
-m: 显示memory相关统计数据;
-n: 显示network相关统计数据;
-p: 显示process相关统计数据;
-r: 显示io请求相关的统计数据;
-s: 显示swapped相关的统计数据;
--tcp
--udp
--unix
--raw
--socket
--ipc
--top-cpu:显示最占用CPU的进程;
--top-io: 显示最占用io的进程;
--top-mem: 显示最占用内存的进程;
--top-lantency: 显示延迟最大的进程;
[[email protected] ~]# dstat -d
-dsk/total-
read writ
3752B 6480B
0 0
0 0
0 0
0 0
[[email protected] ~]# dstat --tcp
----tcp-sockets----
lis act syn tim clo
4 1 0 0 0
4 1 0 0 0
查看最占用io的进程
[[email protected] ~]# dstat --top-io
----most-expensive----
i/o process
bash 4527B 207B
sshd: [email protected] 146B 196B
sshd: [email protected] 78B 116B
查看最占用cpu的进程
[[email protected] ~]# dstat --top-cpu
-most-expensive-
cpu process
events/1 0.0
11、kill命令:
向进程发送控制信号,以实现对进程管理
显示当前系统可用信号:
# kill -l
# man 7 signal
常用信号:
1) SIGHUP: 无须关闭进程而让其重读配置文件;
2) SIGINT: 中止正在运行的进程;相当于Ctrl+c;
9) SIGKILL: 杀死正在运行的进程;
15) SIGTERM:终止正在运行的进程;
18) SIGCONT:
19) SIGSTOP:
指定信号的方法:
(1) 信号的数字标识;1, 2, 9
(2) 信号完整名称;SIGHUP
(3) 信号的简写名称;HUP
向进程发信号:
kill [-SIGNAL] PID...
终止“名称”之下的所有进程:
killall [-SIGNAL] Program
启动一个web服务
yum -y install httpd
[[email protected] ~]# chkconfig httpd on
[[email protected] ~]# service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for MyServer
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ps -ef | grep 80
root 30670 24755 0 21:46 pts/0 00:00:00 grep --color=auto 80
[[email protected] ~]# service httpd status
httpd (pid 30659) is running...
查看监听80端口
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
或者
[[email protected] ~]# netstat -anp | grep 80
tcp 0 0 :::80 :::* LISTEN 30659/httpd
unix 3 [ ] STREAM CONNECTED 8980 1109/master
centos7 systemctl = service +chkconfig
[[email protected] ~]# yum install -y systemctl
Loaded plugins: fastestmirror
Setting up Install Process
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.sina.cn
* extras: centos.ustc.edu.cn
* updates: mirrors.cug.edu.cn
No package systemctl available.
Error: Nothing to do
Linux的作业控制
前台作业:通过终端启动,且启动后一直占据终端;
后台作业:可以通过终端启动,但启动后即转入后台运行(释放终端);
如何让作业运行于后台?
(1) 运行中的作业
Ctrl+z
(2) 尚未启动的作业
# COMMAND &
此类作业虽然被送往后台运行,但其依然与终端相关;如果希望送往后台后,剥离与终端的关系:
# nohup COMMAND &
查看所有作业:
# jobs
作业控制:
# fg [[%]JOB_NUM]:把指定的后台作业调回前台;
# bg [[%]JOB_NUM]:让送往后台的作业在后台继续运行;
# kill [%JOB_NUM]:终止指定的作业;
进程优先级调整:
静态优先级:100-139
进程默认启动时的nice值为0,优先级为120;
nice命令:
nice [OPTION] [COMMAND [ARG]...]
renice命令:
renice [-n] priority pid...
查看:
ps axo pid,comm,ni
未涉及到的命令:sar, tsar, iostat, iftop