当前位置: 移动技术网 > 科技>操作系统>Linux > Linux 进程管理

Linux 进程管理

2019年08月28日  | 移动技术网科技  | 我要评论

金如熙,bleeding love中文歌词,qq字头像

centos-logo

windows有任务管理器来管理进程,linux也有相应的命令来管理进程。


查看进程

ps

  • 查看静态的进程统计信息

a:显示当前终端下的所有进程信息,包括其他用户的进程。与x选项结合时将显示系统中所有的进程信息。
u:使用以用户为主的格式输出进程信息。
x:显示当前用户在所有终端下的进程信息。
-e:显示系统内的所有进程信息。
-l:使用长(long)格式显示进程信息。
-f:使用完整的(full)格式显示进程信息。

[root@localhost ~]# ps aux
user        pid %cpu %mem    vsz   rss tty      stat start   time command
root          1  0.0  0.3 128164  6832 ?        ss   15:50   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2  0.0  0.0      0     0 ?        s    15:50   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        s    15:50   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        s<   15:50   0:00 [kworker/0:0h]
root          6  0.0  0.0      0     0 ?        s    15:50   0:00 [kworker/u256:0]
root          7  0.0  0.0      0     0 ?        s    15:50   0:00 [migration/0]
//有很多,不写。

user:启动该进程的用户账号的名称。
pid:该进程在系统中的数字id号,在当前系统中是唯一的。
%cpucpu占用的百分比。
%mem:内存占用的百分比。
vsz:占用虚拟内存(swap空间)的大小。
rss:占用常驻内存(物理内存)的大小。
tty:表明该进程在哪个终端上运行。?表示未知或不需要终端。
stat:显示进程当前的状态,如s可中断休眠进程d不可中断休眠进程r运行z僵死<高优先级n低优先级s父进程l多线性进程+前台进程。对处于僵死状态的进程应该予以手动终止。
start:启动该进程的时间。
time:该进程占用的cpu时间。
command:启动该进程的命令的名称。

[root@localhost ~]# ps -elf
f s uid         pid   ppid  c pri  ni addr sz wchan  stime tty          time cmd
4 s root          1      0  0  80   0 - 32041 ep_pol 15:50 ?        00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
1 s root          2      0  0  80   0 -     0 kthrea 15:50 ?        00:00:00 [kthreadd]
1 s root          3      2  0  80   0 -     0 smpboo 15:50 ?        00:00:00 [ksoftirqd/0]
1 s root          5      2  0  60 -20 -     0 worker 15:50 ?        00:00:00 [kworker/0:0h]
1 s root          6      2  0  80   0 -     0 worker 15:50 ?        00:00:00 [kworker/u256:0]
1 s root          7      2  0 -40   - -     0 smpboo 15:50 ?        00:00:00 [migration/0]
//有很多,不写。

ppid:当前进程的父进程
ccpu占用
pri:用户态的进程优先级
ni:内核态的进程优先级,取值范围-20~19,数值越低,优先级越高。
addr-表示正在运行
sz:虚拟内存swap占用
wchan:当前进程在内核中的名称

top

  • 查看进程动态信息,每3s刷新一次
[root@localhost ~]# top
top - 19:27:16 up  3:36,  1 user,  load average: 0.00, 0.01, 0.05
tasks:  98 total,   1 running,  97 sleeping,   0 stopped,   0 zombie
%cpu(s):  0.0 us,  6.2 sy,  0.0 ni, 93.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
kib mem :  1867024 total,  1348360 free,   148256 used,   370408 buff/cache
kib swap:  2097148 total,  2097148 free,        0 used.  1525688 avail mem

   pid user      pr  ni    virt    res    shr s %cpu %mem     time+ command
     1 root      20   0  128164   6832   4064 s  0.0  0.4   0:02.43 systemd
     2 root      20   0       0      0      0 s  0.0  0.0   0:00.00 kthreadd
     3 root      20   0       0      0      0 s  0.0  0.0   0:00.25 ksoftirqd/0
     5 root       0 -20       0      0      0 s  0.0  0.0   0:00.00 kworker/0:0h
     6 root      20   0       0      0      0 s  0.0  0.0   0:00.59 kworker/u256:0
     7 root      rt   0       0      0      0 s  0.0  0.0   0:00.00 migration/0
//有很多,不写。

tasks系统任务信息:total总进程数,running正在运行的进程数,sleeping休眠的进程数,stopped中止的进程数,zombie僵死无响应的进程数。

cpu占用信息:us用户占用,sy内核占用,ni优先级调度占用,id空闲cpu,wai/o等待占用,hi硬件中断占用,si软件中断占用,st虚拟化占用。

mem内存占用信息:total总内存空间,free空闲内存,used已用内存,buff/cache,物理内存和交换内存的缓冲区总和。

swap交换空间占用:total总交换空间,free空闲交换空间,used已用交换空间,avail mem可用物理空间。

pgrep

  • 过滤查询进程信息

-l:同时输出对应的进程名,否则只输出pid,不便于查看。
-u:查询特定用户的进程
-t:查询特定终端运行的进程

[root@localhost ~]# pgrep -l "login"
39654 systemd-logind
[root@localhost ~]# pgrep -l -u root -t tty1
40422 x

pstree

  • 查看进程树

-a:列出完整的命令信息
-u:列出对应的用户名
-p:列出对应的pid

[root@localhost ~]# pstree -aup
systemd,1 --switched-root --system --deserialize 21
  ├─modemmanager,39616
  │   ├─{modemmanager},39631
  │   └─{modemmanager},39649
  ├─networkmanager,39701 --no-daemon
  │   ├─dhclient,39841 -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens33.pid -lf...
  │   ├─{networkmanager},39704
  │   └─{networkmanager},39709
//有很多,不写。
  • 不加u,以指定用户作为参数,可查看属于指定用户的进程数结构。
[root@localhost ~]# pstree -ap ll
未发现进程。

控制进程

&

  • 运行较长时间的操作时,命令后面加&符号,放到后台运行
[root@localhost ~]# dd if=/dev/zero of=~/test.tmp bs=1m count=2048 &
[1] 8773

ctrl + c

  • 终止正在执行的进程
[root@localhost ~]# ping www.baidu.com
ping www.a.shifen.com (183.232.231.174) 56(84) bytes of data.
64 bytes from 183.232.231.174 (183.232.231.174): icmp_seq=1 ttl=128 time=34.6 ms
64 bytes from 183.232.231.174 (183.232.231.174): icmp_seq=2 ttl=128 time=34.9 ms
^c
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 34.660/34.823/34.987/0.248 ms

ctrl + z

  • 将前台正在执行的进程调入后台并暂停执行
[root@localhost ~]# dd if=/dev/zero of=~/data.tmp bs=1m count=2048
^z
[1]+  stopped                 dd if=/dev/zero of=~/data.tmp bs=1m count=2048

jobs

  • 查看当前终端在后台运行的进程任务

-l:显示进程对应的pid

[root@localhost ~]# jobs -l
[1]+  8778 stopped                 dd if=/dev/zero of=~/data.tmp bs=1m count=2048

bg

  • 将后台中暂停执行的任务恢复运行,并继续在后台运行,需指定任务编号作为参数
[root@localhost ~]# bg 1

fg

  • 将后台中的任务恢复到前台运行,需指定任务编号作为参数
[root@localhost ~]# fg 1

kill

  • 通过pid终止进程运行,无特定选项时,给程序发送终止信号并正常退出运行

-9:强制终止

[root@localhost ~]# vi testfile

[1]+  stopped                 vi testfile
[root@localhost ~]# jobs -l
[1]+  8935 stopped                 vi testfile
[root@localhost ~]# kill 8935
[root@localhost ~]# jobs -l
[1]+  8935 stopped                 vi testfile
[root@localhost ~]# kill -9 8935
[root@localhost ~]# jobs -l
[1]+  8935 killed                  vi testfile

killall

  • 通过进程名终止运行,需要结束多个相同名称的进程时,killall更方便。
[root@localhost ~]# vi testfile1

[1]+  已停止               vi testfile1
[root@localhost ~]# vi testfile2

[2]+  已停止               vi testfile2
[root@localhost ~]# jobs -l
[1]-  1803 停止                  vi testfile1
[2]+  1804 停止                  vi testfile2
[root@localhost ~]# killall -9 vi
[1]-  已杀死               vi testfile1
[2]+  已杀死               vi testfile2
[root@localhost ~]# jobs -l

pkill

  • 特定条件终止,与pgrep类似

-u:指定用户
-t:指定终端

[root@localhost ~]# pgrep -l -u "ll"
1875 gnome-keyring-d
1893 gnome-session-b
//有很多,不写
[root@localhost ~]# pkill -9 -u "ll"
[root@localhost ~]# pgrep -l -u "ll"

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网