1、进程定义
进程:程序被触发后,执行者的权限与属性、程序的程序码与所需数据等都会被载入内存中,操作系统并给予这个内存内的单元一个识别码(PID),可以说,进程就是cpu未完成的工作,或者就是一个正在运行中的程序。
进程状态:
R(Running):该程序正在运行中
S(Sleep):该程序目前正在睡眠状态,但可以被唤醒
D :不可以被唤醒的睡眠状态,通常进程可能在等待 I/O 的情况
T :停止状态(stop),可能是工作控制(背景暂停)或除错状态
Z(Zombie):僵尸状态,程序已经终止但无法被移除至内存外
2、ps命令
ps a #关于当前环境的所有进程
[root@localhost ~]# ps a
PID TTY STAT TIME COMMAND
590 tty1 Ss+ 0:00 /usr/bin/Xorg :0 -background none -verbose -auth /run
1162 ttyS0 Ss+ 0:00 /sbin/agetty --keep-baud ttyS0 115200 38400 9600
2113 pts/0 Ss 0:00 -bash
2733 pts/0 R+ 0:00 ps a
ps x| -A #所有进程
ps f #显示进程从属关系
ps e #显示进程调用环境工具的详细信息
ps l #长列表显示进程的详细信息
ps u #显示进程的用户信息
ps -a #显示shell前台运行命令的进程,但不保函shell本身
ps ax -o %cpu,%mem,user,group,comm,nice,pid,stat #指定显示进程的某些信息
%cpu #显示进程cpu负载
%mem #显示进程内存负载
user #进程用户
group #进程组
comm #进程名称
nice #进程优先级
pid #进程的id
stat #进程状态
stat中显示的信息
S #进程状态
l #内存中有锁定空间
N #优先级低
< #优先级高
+ #前台运行
s #顶级进程
[root@localhost ~]# ps ax -o %cpu,%mem,user,group,comm,nice,pid,stat%CPU %MEM USER GROUP COMMAND NI PID STAT
0.0 0.4 root root systemd 0 1 Ss
0.0 0.0 root root kthreadd 0 2 S
0.0 0.0 root root ksoftirqd/0 0 3 S
0.0 0.0 root root kworker/0:0H -20 5 S<
0.0 0.0 root root migration/0 - 7 S
0.0 0.0 root root rcu_bh 0 8 S
0.0 0.0 root root rcuob/0 0 9 S
0.0 0.0 root root rcu_sched 0 10 R
0.0 0.0 root root rcuos/0 0 11 S
0.0 0.0 root root watchdog/0 - 12 S
0.0 0.0 root root khelper -20 13 S<
0.0 0.0 root root kdevtmpfs 0 14 S
0.0 0.0 root root netns -20 15 S<
0.0 0.0 root root writeback -20 16 S<
0.0 0.0 root root kintegrityd -20 17 S<
0.0 0.0 root root bioset -20 18 S<
0.0 0.0 root root kblockd -20 19 S<
0.0 0.0 root root khubd 0 20 S
0.0 0.0 root root md -20 21 S<
0.0 0.0 root root kswapd0 0 24 S
0.0 0.0 root root ksmd 5 25 SN
0.0 0.0 root root khugepaged 19 26 SN
0.0 0.0 root root fsnotify_mark 0 27 S
0.0 0.0 root root crypto -20 28 S<
0.0 0.0 root root kthrotld -20 37 S<
0.0 0.0 root root kworker/u2:1 0 38 S
0.0 0.0 root root kmpath_rdacd -20 39 S<
0.0 0.0 root root kpsmoused -20 40 S<
0.0 0.0 root root deferwq -20 60 S<
0.0 0.0 root root kauditd 0 84 S
0.0 0.0 root root rpciod -20 165 S<
0.0 0.0 root root ata_sff -20 224 S<
0.0 0.0 root root kworker/u2:2 0 227 S
0.0 0.0 root root scsi_eh_0 0 228 S
0.0 0.0 root root scsi_tmf_0 -20 229 S<
0.0 0.0 root root scsi_eh_1 0 230 S
0.0 0.0 root root scsi_tmf_1 -20 231 S<
0.0 0.0 root root xfsalloc -20 251 S<
0.0 0.0 root root xfs_mru_cache -20 252 S<
0.0 0.0 root root xfslogd -20 253 S<
0.0 0.0 root root xfs-data/vda1 -20 254 S<
0.0 0.0 root root xfs-conv/vda1 -20 255 S<
0.0 0.0 root root xfs-cil/vda1 -20 256 S<
0.0 0.0 root root kworker/0:1H -20 257 S<
0.0 0.0 root root xfsaild/vda1 0 258 S
0.0 0.2 root root systemd-journal 0 365 Ss
0.0 0.1 root root lvmetad 0 373 Ss
0.0 0.2 root root systemd-udevd 0 392 Ss
0.0 0.0 root root vballoon 0 411 S
0.0 0.0 root root ttm_swap -20 424 S<
0.0 0.0 root root kdmflush -20 425 S<
0.0 0.0 root root qxl_gc -20 426 S<
0.0 0.0 root root bioset -20 427 S<
0.0 0.0 root root jbd2/dm-0-8 0 440 S
0.0 0.0 root root ext4-dio-unwrit -20 441 S<
0.0 0.1 root root auditd -4 445 S<sl
0.0 0.0 root root audispd -8 458 S<sl
0.0 0.0 root root sedispatch -4 467 S<
0.0 0.1 root root alsactl 19 471 SNs
0.0 2.1 root root firewalld 0 473 Ssl
0.0 0.1 avahi avahi avahi-daemon 0 478 Ss
0.0 1.6 root root tuned 0 479 Ssl
0.0 0.3 root root rsyslogd 0 482 Ssl
0.0 0.5 root root abrtd 0 492 Ss
0.0 0.4 root root abrt-watch-log 0 493 Ss
0.0 0.4 root root abrt-watch-log 0 495 Ss
0.0 0.0 avahi avahi avahi-daemon 0 497 S
0.0 0.1 chrony chrony chronyd 0 499 S
0.0 0.0 libstor+ libstor+ lsmd 0 500 Ss
0.0 0.4 root root ModemManager 0 511 Ssl
0.0 0.2 root root smartd 0 515 Ss
0.0 0.1 root root systemd-logind 0 517 Ss
0.0 0.5 root root accounts-daemon 0 518 Ssl
0.0 0.0 root root iprupdate 0 520 Ss
0.0 0.1 rtkit rtkit rtkit-daemon 1 521 SNsl
0.0 0.2 dbus dbus dbus-daemon 0 530 Ss
0.0 0.1 root root crond 0 545 Ss
0.0 0.0 root root atd 0 546 Ss
0.0 0.3 root root gdm 0 547 Ssl
0.0 0.0 root root iprinit 0 550 Ss
0.0 0.0 root root ksmtuned 0 557 S
0.0 0.0 root root iprdump 0 567 Ss
0.0 0.6 root root gdm-simple-slav 0 579 Sl
0.0 1.4 polkitd polkitd polkitd 0 581 Ssl
0.0 1.8 root root Xorg 0 590 Ss+
0.0 0.9 root root NetworkManager 0 665 Ssl
0.0 0.4 root gdm gdm-session-wor 0 735 Sl
0.0 1.2 gdm gdm gnome-session 0 924 Ssl
0.0 0.0 gdm gdm dbus-launch 0 933 S
0.0 0.1 gdm gdm dbus-daemon 0 998 Ss
0.0 1.1 root root libvirtd 0 1145 Ssl
0.0 0.3 root root sshd 0 1152 Ss
0.0 0.0 root root agetty 0 1162 Ss+
0.0 0.1 rpc rpc rpcbind 0 1164 Ss
0.0 0.1 rpcuser rpcuser rpc.statd 0 1207 Ss
0.0 0.3 gdm gdm at-spi-bus-laun 0 1268 Sl
0.0 0.1 gdm gdm dbus-daemon 0 1314 S
0.0 0.3 gdm gdm at-spi2-registr 0 1323 Sl
0.0 2.1 gdm gdm gnome-settings- 0 1357 Sl
0.0 0.4 root root upowerd 0 1398 Ssl
0.0 0.2 root root master 0 1432 Ss
0.0 0.3 postfix postfix pickup 0 1437 S
0.0 0.4 postfix postfix qmgr 0 1438 S
0.2 10.3 gdm gdm gnome-shell 0 1520 Sl
0.0 0.5 colord colord colord 0 1522 Ssl
0.0 0.3 gdm gdm pulseaudio -11 1528 S<l
0.0 0.4 gdm gdm dconf-service 0 1535 Sl
0.0 0.7 gdm gdm ibus-daemon 0 1551 Sl
0.0 0.3 gdm gdm ibus-dconf 0 1553 Sl
0.0 0.8 gdm gdm ibus-x11 0 1557 Sl
0.0 0.3 gdm gdm ibus-engine-sim 0 1575 Sl
0.0 0.6 gdm gdm mission-control 0 1667 Sl
0.0 1.6 gdm gdm goa-daemon 0 1679 Sl
0.0 0.5 root root sshd 0 2110 Ss
0.0 0.3 root root bash 0 2113 Ss
0.0 0.0 root root kworker/0:2 0 2667 S
0.0 0.0 root root kworker/0:0 0 2742 R
0.0 0.0 root root kworker/0:1 0 2850 S
0.0 0.0 root root sleep 0 2874 S
0.0 0.1 root root ps 0 2875 R+
ps ax -o %cpu,comm --sort <+|-%cpu > <+|-%mem> #按照进程信息排序
+ #正序
- #倒序
%cpu #cpu负载排序
%mem #内存负载
[root@localhost ~]# ps ax -o %cpu,nice,pid --sort -%cpu%CPU NI PID
0.2 0 1520
0.0 0 1
0.0 0 2
0.0 0 3
0.0 -20 5
0.0 - 7
0.0 0 8
0.0 0 9
0.0 0 10
0.0 0 11
0.0 - 12
0.0 -20 13
0.0 0 14
0.0 -20 15
0.0 -20 16
0.0 -20 17
0.0 -20 18
0.0 -20 19
0.0 0 20
0.0 -20 21
0.0 0 24
0.0 5 25
0.0 19 26
0.0 0 27
0.0 -20 28
0.0 -20 37
0.0 0 38
0.0 -20 39
0.0 -20 40
0.0 -20 60
0.0 0 84
0.0 -20 165
0.0 -20 224
0.0 0 227
0.0 0 228
0.0 -20 229
0.0 0 230
0.0 -20 231
0.0 -20 251
0.0 -20 252
0.0 -20 253
0.0 -20 254
0.0 -20 255
0.0 -20 256
0.0 -20 257
0.0 0 258
0.0 0 365
0.0 0 373
0.0 0 392
0.0 0 411
0.0 -20 424
0.0 -20 425
0.0 -20 426
0.0 -20 427
0.0 0 440
0.0 -20 441
0.0 -4 445
0.0 -8 458
0.0 -4 467
0.0 19 471
0.0 0 473
0.0 0 478
0.0 0 479
0.0 0 482
0.0 0 492
0.0 0 493
0.0 0 495
0.0 0 497
0.0 0 499
0.0 0 500
0.0 0 511
0.0 0 515
0.0 0 517
0.0 0 518
0.0 0 520
0.0 1 521
0.0 0 530
0.0 0 545
0.0 0 546
0.0 0 547
0.0 0 550
0.0 0 557
0.0 0 567
0.0 0 579
0.0 0 581
0.0 0 590
0.0 0 665
0.0 0 735
0.0 0 924
0.0 0 933
0.0 0 998
0.0 0 1145
0.0 0 1152
0.0 0 1162
0.0 0 1164
0.0 0 1207
0.0 0 1268
0.0 0 1314
0.0 0 1323
0.0 0 1357
0.0 0 1398
0.0 0 1432
0.0 0 1437
0.0 0 1438
0.0 0 1522
0.0 -11 1528
0.0 0 1535
0.0 0 1551
0.0 0 1553
0.0 0 1557
0.0 0 1575
0.0 0 1667
0.0 0 1679
0.0 0 2110
0.0 0 2113
0.0 0 2667
0.0 0 2742
0.0 0 2850
0.0 0 2883
0.0 0 2884
3、进程优先级
1、进程的优先级范围:-20~19
2、优先级查看
ps ax -o pid,nice,comm
3、指定某个优先级开启进程
nice -n 优先级数字 进程名称
4、改变进程优先级
renice -n 优先级数字 进程pid
[root@localhost ~]# ps lF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 0 590 579 20 0 160728 17812 poll_s Ss+ tty1 0:01 /usr/bin/Xo
4 0 1162 1 20 0 110004 820 n_tty_ Ss+ ttyS0 0:00 /sbin/agett
4 0 2113 2110 20 0 116248 2976 wait Ss pts/0 0:00 -bash
0 0 2895 2113 20 0 121260 984 - R+ pts/0 0:00 ps l
[root@localhost ~]# renice -n -6 2113
2113 (process ID) old priority 0, new priority -6
4、环境中进程的前后台调用
jobs #查看被打入环境后台的进程
ctrl+z #把占用终端的进程打入后台
fg jobsnum #把后台进程调回前台
bg jobsnum #把后台暂停的进程运行
comm & #让命令直接在后台运行
5、进程信号
1、常用信号等级
1 #进程重新加载配置
2 #删除进程在内存中的数据
3 #删除鼠标在内存中的数据
9 #强行结束单个进程(不能被阻塞)
15 #正常关闭进程 (可能会被阻塞)
18 #运行暂停的进程
19 #暂停某个进程 (不能被阻塞)
20 #把进程打入后台 (可以被阻塞)
man 7 signal #查看信号详细信息
kill -信号 进程pid #对单个进程操作
killall -信号 进程名字 #对进程进行批量操作
pkill -u student -信号 #对某个用户下的进程进行操作
[root@localhost ~]# ps ax -o pid,comm --sort +pid | tail -n 53049 vim
3068 kworker/0:2
3100 sleep
3103 ps
3104 bash
[root@localhost ~]# kill -9 3049
[root@localhost ~]# ps ax -o pid,comm --sort +pid | tail -n 5
3014 kworker/0:1
3068 kworker/0:2
3112 sleep
3127 ps
3128 tail
[1]+ Killed vim
6、用户登陆审记
w #查看使用系统的当前用户有那些
w -f #-f查看使用地点
last #查看用户登陆成功历史
lastb #查看用户登陆未成功历史
7、top命令
top #监控系统负载工具
s #修改进程刷新间隔时间
top - 06:33:53 up 1:59, 2 users, load average: 0.00, 0.01, 0.04
Tasks: 120 total, 2 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 969416 total, 560472 used, 408944 free, 900 buffers
KiB Swap: 0 total, 0 used, 0 free. 249180 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 50468 3972 2200 S 0.0 0.4 0:01.11 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.00 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.09 rcu_sched
11 root 20 0 0 0 0 R 0.0 0.0 0:00.16 rcuos/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.03 watchdog/0
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
m #根据内存占有率进行排序
top - 06:42:16 up 2:07, 2 users, load average: 0.00, 0.01, 0.04
Tasks: 120 total, 2 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 969416 total, 560268 used, 409148 free, 900 buffers
KiB Swap: 0 total, 0 used, 0 free. 249180 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 50468 3972 2200 S 0.0 0.4 0:01.11 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.00 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.09 rcu_sched
11 root 20 0 0 0 0 R 0.0 0.0 0:00.16 rcuos/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.04 watchdog/0
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
c # 根据 cpu 占有率进行排序
u #后面输入用户名 , 用来查看该用户发起的所有进程
k #后面跟要作用的进程 pid, 和发起的信号 (eg:9,15,20) 用来给某个进程发起一信号
q #退出进程查看界面
h #显示 top 命令的帮助
八、系统初始化程序
1、systemd
系统初始化程序,系统开始的第一个进程,pid为1
2、systemctl 命令
systemctl list-units #列出当前系统服务的状态
[root@localhost ~]# systemctl list-unitsUNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys...t_misc.automount loaded active waiting Arbitrary Executable File Fo
sys-devi...-net-eth0.device loaded active plugged Virtio network device
sys-devi...-vda-vda1.device loaded active plugged /sys/devices/pci0000:00/0000
sys-devi...block-vda.device loaded active plugged /sys/devices/pci0000:00/0000
sys-devi...-vdb-vdb1.device loaded active plugged LVM PV SIaPf4-OdHu-OzAW-NlQG
sys-devi...block-vdb.device loaded active plugged /sys/devices/pci0000:00/0000
sys-devi...tty-ttyS1.device loaded active plugged /sys/devices/platform/serial
sys-devi...tty-ttyS2.device loaded active plugged /sys/devices/platform/serial
sys-devi...tty-ttyS3.device loaded active plugged /sys/devices/platform/serial
sys-devi...tty-ttyS0.device loaded active plugged /sys/devices/pnp0/00:04/tty/
sys-devi...k-dm\x2d0.device loaded active plugged /sys/devices/virtual/block/d
sys-module-configfs.device loaded active plugged /sys/module/configfs
sys-subs...ices-eth0.device loaded active plugged Virtio network device
-.mount loaded active mounted /
dev-hugepages.mount loaded active mounted Huge Pages File System
dev-mqueue.mount loaded active mounted POSIX Message Queue File Sys
home.mount loaded active mounted /home
proc-fs-nfsd.mount loaded active mounted RPC Pipe File System
systemctl list-unit-files #列出服务的开机状态
[root@localhost ~]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount masked
var-lib-nfs-rpc_pipefs.mount static
brandbot.path disabled
cups.path enabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-6.scope static
session-c1.scope static
abrt-ccpp.service enabled
lines 1-19
systemctl status sshd #查看指定服务的状态
systemctl stop sshd #关闭指定服务
systemctl start sshd #开启指定服务
systemctl restart sshd #重新启动服务
systemctl enable sshd #设定指定服务开机开启(需要重启reboot)
systemctl disable sshd #设定指定服务开机关闭(需要重启reboot)
systemctl reload sshd #使指定服务重新加载配置
systemctl list-dependencies sshd #查看指定服务的依赖关系
systemctl mask sshd #冻结指定服务
[root@localhost ~]# systemctl status sshd
sshd.service
Loaded: masked (/dev/null)
Active: active (running) since Wed 2018-04-25 04:34:54 EDT; 2h 12min ago
Main PID: 1152 (sshd)
CGroup: /system.slice/sshd.service
└─1152 /usr/sbin/sshd -D
Apr 25 04:34:54 localhost systemd[1]: Started OpenSSH server daemon.
Apr 25 04:34:54 localhost sshd[1152]: Server listening on 0.0.0.0 port 22.
Apr 25 04:34:54 localhost sshd[1152]: Server listening on :: port 22.
Apr 25 04:35:02 localhost sshd[1319]: Accepted password for root from 172.2...h2
Apr 25 05:05:44 localhost sshd[2110]: Accepted password for root from 172.2...h2
Hint: Some lines were ellipsized, use -l to show in full.
systemctl unmask sshd #启用服务
[root@localhost ~]# systemctl unmask sshd
[root@localhost ~]# systemctl status sshd
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since Wed 2018-04-25 04:34:54 EDT; 2h 13min ago
Main PID: 1152 (sshd)
CGroup: /system.slice/sshd.service
└─1152 /usr/sbin/sshd -D
Apr 25 04:34:54 localhost systemd[1]: Started OpenSSH server daemon.
Apr 25 04:34:54 localhost sshd[1152]: Server listening on 0.0.0.0 port 22.
Apr 25 04:34:54 localhost sshd[1152]: Server listening on :: port 22.
Apr 25 04:35:02 localhost sshd[1319]: Accepted password for root from 172.2...h2
Apr 25 05:05:44 localhost sshd[2110]: Accepted password for root from 172.2...h2
Hint: Some lines were ellipsized, use -l to show in full.
systemctl set-default multi-user.target #开机不开启图形(重启时非图形界面)
重起后可安init5恢复画面
systemctl set-default graphical.target #开机启动图形
setterm #文本界面设定color
vga=ask #改分辨率
3、服务状态
systemctl status 服务名称
loaded #系统服务已经初始化完成,加载过配置
active(running) #正有一个或多个程序正在系统中执行
atcive(exited) #仅执行一次就正常结束的服务
atcive(waiting) #正在执行当中,不过还在等待其他的事件才能继续处理
inactive #服务关闭
enbaled #服务开机启动
disabled #服务开机不自启
static #服务开机启动项不可被管理
failed #系统配置错误