进程

一.查看进程
1.定义:
默认进程:当前用户的进程
systemd:系统初始化进程
过儿进程受系统初始化进程控管

2、查看进程
(1)图形方式查看
图形进程命令:只适用于图形开启的情况下
命令:gnome-system-monitor
打开后会进入到进程图形界面
2、进程查看命令
没有图形工具时,需用命令(ps)
man ps----查看ps命令的说明界面
( 1) ps -A #查看所有进程

[[email protected] ~]# ps -A
PID TTY TIME CMD
1 ? 00:00:01 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
7 ? 00:00:00 migration/0
8 ? 00:00:00 rcu_bh
9 ? 00:00:00 rcuob/0
10 ? 00:00:00 rcuob/1
11 ? 00:00:00 rcuob/2
12 ? 00:00:00 rcuob/3

(2) ps -a #在当前环境下运行进程

[[email protected] ~]# ps -a
PID TTY TIME CMD
11692 pts/0 00:00:00 su
11695 pts/0 00:00:00 bash
12331 pts/0 00:00:00 firefox
12348 pts/0 00:00:00 firefox
18185 pts/0 00:00:00 ps

firefox & 让进程再后台运行
ps 默认在当前进行的前台进行的进程,只能看到进程名字
ps -e 进程详细信息和占用系统资源
ps -A 没有条件的所有进程信息 (TTY输出终端)
ps x 有输出终端(TTY)的进程,没有输出设备的不显示

3、ps -u ##显示进程用户信息 (user)
(1)root用户

[ro[email protected] ~]# ps -u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2397 0.9 2.1 421876 80564 tty1 Ssl+ 08:41 2:29 /usr/bin/Xorg : ##当前为root用户
root 11692 0.0 0.0 190360 2656 pts/0 S 10:37 0:00 su - root
root 11695 0.0 0.0 116164 2872 pts/0 S 10:37 0:00 -bash
root 12331 0.0 0.7 489036 26524 pts/0 T 10:43 0:00 /usr/lib64/fire
root 12348 0.0 0.0 0 0 pts/0 Z 10:43 0:00 [firefox] <defu
root 18230 0.0 0.0 139492 1636 pts/0 R+ 13:05 0:00 ps -u

(2)kiosk用户

[[email protected] ~]# su - kiosk ##进入kiosk用户
Last login: Wed Oct 3 08:41:51 CST 2018 on :0
[[email protected] ~]$ ps -u ##查看进程用户信息
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
kiosk 11104 0.0 0.0 116284 2928 pts/0 Ss 10:28 0:00 /bin/bash
kiosk 18305 0.2 0.0 116164 2852 pts/0 S 13:09 0:00 -bash
kiosk 18355 0.0 0.0 139492 1640 pts/0 R+ 13:09 0:00 ps -u

4 、 ps a ##在当前环境运行的进程

[[email protected] ~]# ps a
PID TTY STAT TIME COMMAND
2397 tty1 Ssl+ 2:40 /usr/bin/Xorg :0 -background none -noreset -audit 4 -
11104 pts/0 Ss 0:00 /bin/bash
11692 pts/0 S 0:00 su - root
11695 pts/0 S 0:00 -bash
12331 pts/0 T 0:00 /usr/lib64/firefox/firefox
12348 pts/0 Z 0:00 [firefox]
18659 pts/0 R+ 0:00 ps a

注意:当前环境运行的进程与用户无关
切换用户到kiosk ,可看出进程相同

[[email protected] ~]# su - kiosk
Last login: Wed Oct 3 13:09:44 CST 2018 on pts/0
[[email protected] ~]$ ps a
PID TTY STAT TIME COMMAND
2397 tty1 Ssl+ 2:41 /usr/bin/Xorg :0 -background none -noreset -audit 4 -
11104 pts/0 Ss 0:00 /bin/bash
11692 pts/0 S 0:00 su - root
11695 pts/0 S 0:00 -bash
12331 pts/0 T 0:00 /usr/lib64/firefox/firefox
12348 pts/0 Z 0:00 [firefox]
18676 pts/0 S 0:00 su - kiosk
18677 pts/0 S 0:00 -bash
18718 pts/0 R+ 0:00 ps a

5、 ps x 显示有输出终端(TTY)的进程,没有输出设备的不显示
6 、 ps f 显示父子进程

[[email protected] ~]# ps f
PID TTY STAT TIME COMMAND
3065 pts/0 S 0:00 su - root
3068 pts/0 S 0:00 _ -bash ##root的子进程
8235 pts/0 R+ 0:00 _ ps f ##bash的子进程
2397 tty1 Ssl+ 0:35 /usr/bin/Xorg :0 -background none -noreset -audit 4 -
6802 pts/0 S 0:00 dbus-launch --autolaunch=12e4220f7c274a96a0da1f9a7717
6798 pts/0 Sl 0:01 virt-viewer desktop

7 、 pstree 显示当前系统的进程树

8、 ps常用组合
(1) ps aux #显示进程所有进程并显示进程用户

[[email protected] ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.2 125400 3924 ? Ss 21:44 0:01 /usr/lib/system
root 2 0.0 0.0 0 0 ? S 21:44 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 21:44 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 21:44 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S 21:44 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 21:44 0:00 [rcu_bh]
root 9 0.1 0.0 0 0 ? S 21:44 0:00 [rcu_sched]
root 10 0.0 0.0 0 0 ? S 21:44 0:00 [watchdog/0]
root 11 0.0 0.0 0 0 ? S 21:44 0:00 [watchdog/1]
root 12 0.0 0.0 0 0 ? S 21:44 0:00 [migration/1]
root 13 0.0 0.0 0 0 ? S 21:44 0:00 [ksoftirqd/1]

(2) ps ef ##显示进程详细信息并显示进程父子关系

[[email protected] ~]$ ps ef
PID TTY STAT TIME COMMAND
2604 pts/0 Ss 0:00 bash SESSION_MANAGER=local/unix:@/tmp/.ICE-unix/1473,
2870 pts/0 R+ 0:00 _ ps ef XDG_VTNR=1 SSH_AGENT_PID=1646 XDG_SESSION_I
[[email protected] ~]$

(3)、ps ax ##显示当前系统的所有进程。
二 、ps -o 显示进程指定信息

1、ps -o xxx 显示进程的指定信息

(1) ps -o comm ##进程名称

[[email protected] ~]$ ps -o comm
COMMAND
bash
ps

(2)ps -o user ##显示进程所有人

[[email protected] ~]$ ps -o user
USER
kiosk
kiosk

(3) ps -o group ## 进程所有组

[[email protected] ~]$ ps -o group
GROUP
kiosk
kiosk

(4) ps -o %cpu 显示cpu使用率

[[email protected] ~]$ ps -o %cpu
%CPU
0.0
0.0

(5)ps -o %mem ##显示进程内存使用率
[[email protected] ~]$ ps -o %mem
%MEM
0.1
0.0

(6)ps -o nice ##显示进程优先级
(7)ps -o 可同时显示多个指定进程信息
例如:显示cpu ,进程名称及进程id

[[email protected] ~]$ ps -o %cpu,comm,pid
%CPU COMMAND PID
0.0 bash 2604
0.0 ps 3249

2.firefox的资源系统是无法在其休眠期间回收的,因为firefox有缓存的图形。
3、按顺序对进程进行排列并显示
ps --sort %cpu -o %cpu,comm #升序对cpu使用率进行排序并显示cpu使用率及进程名称
练习:显示CPU使用前五进程的id

[[email protected] ~]# ps ax --sort -%cpu,pid -o pid |head -n 6|tail -n5
2503
6677
2397
9355
487

三.stat中显示的信息
s ##进程状态
l ##内存中有锁定空间
N ##优先级低
< ##优先级高
+加号 ##前台运行
s ##*进程

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 125400 3924 ? Ss 21:44 0:02 /usr/lib/system
root 5 0.0 0.0 0 0 ? S< 21:44 0:00 [kworker/0:0H]
root 33 0.0 0.0 0 0 ? SN 21:44 0:00 [ksmd]
root 34 0.0 0.0 0 0 ? SN 21:44 0:00 [khugepaged]
root 673 0.0 0.1 55416 1720 ? S<sl 21:44 0:00 /sbin/auditd -n
rtkit 707 0.0 0.0 164648 1272 ? SNsl 21:44 0:00 /usr/libexec/rt ##此进程为*进程,但优先级低,内存中有锁定空间。
root 1411 2.5 2.4 296444 35756 tty1 Rsl+ 21:44 1:04 /usr/bin/Xorg :
root 1701 0.0 0.5 371984 8312 ? Ssl 21:44 0:00 /usr/libexec/up
kiosk 1723 5.4 9.0 1904008 134528 ? Sl 21:44 2:18 /usr/bin/gnome-
colord 1725 0.0 0.4 404296 6020 ? Ssl 21:44 0:00 /usr/libexec/co
root 1726 0.0 0.3 510376 5640 ? Ssl 21:44 0:00 /usr/lib/udisks

四.进程优先级

1、 进程优先级范围
-20~19 (优先级从高到底)

2、查看优先级(进程数为0时进程是默认正常的进程数)
ps -o nice,comm 查看进程优先级并显示进程

[[email protected] ~]# ps -o nice,pid,comm
NI PID COMMAND
0 3065 su
0 3068 bash
0 6798 virt-viewer
0 6802 dbus-launch
0 10510 ps

3、 指定某个优先级开启进程
格式:nice -n 优先级数字 进程名称
nice -n -5 vim & ##开启vim并且指定程序优先级为-5

4.改变进程优先级
renice -n 数字 进程id(pid)
将进程pid为3068的进程优先级改为-5

[[email protected] ~]# renice -n -5 3068
3068 (process ID) old priority 0, new priority -5

5、进程调用
ctrl + c 结束进程
实验
(1)进入图文工具,并且打开firefox

[[email protected] ~]# firefox ##打开火狐浏览器(测试使用,前提:以手动在页面打开firefox)
[[email protected] ~]# gedit ##进入图文工具(测试使用)

(2).ctrl+z ##把占有终端的进程打入后台

[[email protected] ~]# gedit
(gedit:11747): Gtk-WARNING **: Calling Inhibit failed: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files
^Z ##将图文工具打入后台Ctrl+Z
[1]+ Stopped gedit
[[email protected] ~]# firefox
^Z ##将firefox打入后台
[2]+ Stopped firefox

(3)、jobs ##查看被打入环境后台的进程

[[email protected] ~]# jobs
[1]- Stopped gedit
[2]+ Stopped firefox

(4)、fg jobsnum ##把后台进程调回前台

[[email protected] ~]# fg 1
gedit

此时gedit是可以编辑的。
注意:当fg 后面不加jobsnum时,默认先恢复的是+的进程([2]+ Stopped firefox)
(5)comm & #让命令直接在后台运行

五、系统信号
1.man 7 signal (查看信号等级,q退出查看)
进程
常用信号等级
1 ##进程重新加载(重新加载配置而不关闭进程)
2 ##删除进程在内存中的数据
3 ##删除鼠标在内存中的数据
9 ##强行结束单个进程(不能被阻塞即执行命令一定会结束)
15 ##正常关闭进程(可能被阻塞,及执行命令进程可能不会结束)
18 ##运行暂停的进程
19 ##暂停某个进程(不能被阻塞)
20 ##把进程打入后台(可以被阻塞)

2、对进程进行操作(kill)
kill -信号 进程pid
killall -信号 进程名称
pkill -u student -信号
SIGHUP 终止进程 终端线路挂断。

四.###top##
top 可以看到所有进程
top进入进程信息显示页面后:
进程

按1 ##显示cpu每个核的负载
按s ##调整刷新频率(按完s后直接输入需要调整的频率指回车,刷新频率调整 成功)
进程
进程
按c ##CPU负载排序
按m ##内存使用量排序
按h ##查看帮助
按u ##查看指定用户进程
按k ##对指定进程发起信号
按q ##推出进程显示页面