Linux 管理员 - 进程管理
以下是进程管理常用的命令 - bg、fg、nohup、ps、pstree、top、kill、killall、free、uptime、nice。
处理进程
快速说明 − Linux 中的进程 PID
在 Linux 中,每个正在运行的进程都会被赋予一个 PID 或进程 ID 号。这个 PID 就是 CentOS 识别特定进程的方式。正如我们所讨论的,systemd 是 CentOS 中第一个启动并被赋予 PID 1 的进程。
Pgrep 用于获取给定进程名称的 Linux PID。
[root@CentOS]# pgrep systemd 1 [root@CentOS]#
如所见,pgrep 命令返回 systemd 的当前 PID。
CentOS 中的基本 CentOS 进程和作业管理
在 Linux 中使用进程时,了解在命令行中如何执行基本的前台和后台进程非常重要。
fg − 将进程带到前台
bg −将进程移至后台
jobs − 当前连接到 shell 的进程列表
ctrl+z − Control + z 组合键使当前进程休眠
& − 在后台启动进程
让我们开始使用 shell 命令 sleep。sleep 的作用正如其名称所示,即休眠一段规定的时间 − sleep。
[root@CentOS ~]$ jobs [root@CentOS ~]$ sleep 10 & [1] 12454 [root@CentOS ~]$ sleep 20 & [2] 12479 [root@CentOS ~]$ jobs [1]- Running sleep 10 & [2]+ Running sleep 20 & [cnetos@CentOS ~]$
现在,让我们将第一个作业带到前台 −
[root@CentOS ~]$ fg 1 sleep 10
如果您一直在关注,您会注意到前台作业卡在您的 shell 中。现在,让我们将进程置于休眠状态,然后在后台重新启用它。
- 按下 control+z
- 输入:bg 1,将第一个作业发送到后台并启动它。
[root@CentOS ~]$ fg 1 sleep 20 ^Z [1]+ Stopped sleep 20 [root@CentOS ~]$ bg 1 [1]+ sleep 20 & [root@CentOS ~]$
nohup
使用 shell 或终端时,值得注意的是,默认情况下,当 shell 关闭或用户注销时,附加到 shell 的所有进程和作业都将终止。使用 nohup 时,如果用户注销或关闭进程所附加到的 shell,进程将继续运行。
[root@CentOS]# nohup ping www.google.com & [1] 27299 nohup: ignoring input and appending output to ‘nohup.out’ [root@CentOS]# pgrep ping 27299 [root@CentOS]# kill -KILL `pgrep ping` [1]+ Killed nohup ping www.google.com [root@CentOS rdc]# cat nohup.out PING www.google.com (216.58.193.68) 56(84) bytes of data. 64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 1 ttl = 128 time = 51.6 ms 64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 2 ttl = 128 time = 54.2 ms 64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 3 ttl = 128 time = 52.7 ms
ps 命令
ps 命令通常由管理员用于调查特定进程的快照。ps 通常与 grep 一起使用,以筛选出要分析的特定进程。
[root@CentOS ~]$ ps axw | grep python 762 ? Ssl 0:01 /usr/bin/python -Es /usr/sbin/firewalld --nofork -nopid 1296 ? Ssl 0:00 /usr/bin/python -Es /usr/sbin/tuned -l -P 15550 pts/0 S+ 0:00 grep --color=auto python
在上面的命令中,我们看到所有使用 python 解释器的进程。结果中还包括我们的 grep 命令,用于查找字符串 python。
以下是与 ps 一起使用的最常见命令行开关。
开关 | 操作 |
---|---|
a | 仅排除当前用户的报告进程的约束 |
x | 显示未连接到 tty 或 shell 的进程 |
w | 格式化输出的宽输出显示 |
e | 显示环境后命令 |
-e | 选择所有进程 |
-o | 用户定义格式的输出 |
-u | 显示特定用户的所有进程 |
-C | 按名称或进程 ID 显示所有进程 |
--sort | 按定义对进程进行排序 |
查看 nobody 用户正在使用的所有进程 −
[root@CentOS ~]$ ps -u nobody PID TTY TIME CMD 1853 ? 00:00:00 dnsmasq [root@CentOS ~]$
查看有关 firewalld 进程的所有信息 −
[root@CentOS ~]$ ps -wl -C firewalld F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 0 762 1 0 80 0 - 81786 poll_s ? 00:00:01 firewalld [root@CentOS ~]$
让我们看看哪些进程消耗的内存最多 −
[root@CentOS ~]$ ps aux --sort=-pmem | head -10 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND cnetos 6130 0.7 5.7 1344512 108364 ? Sl 02:16 0:29 /usr/bin/gnome-shell cnetos 6449 0.0 3.4 1375872 64440 ? Sl 02:16 0:00 /usr/libexec/evolution-calendar-factory root 5404 0.6 2.1 190256 39920 tty1 Ssl+ 02:15 0:27 /usr/bin/Xorg :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-iDefCt/database -seat seat0 -nolisten tcp vt1 cnetos 6296 0.0 1.7 1081944 32136 ? Sl 02:16 0:00 /usr/libexec/evolution/3.12/evolution-alarm-notify cnetos 6350 0.0 1.5 560728 29844 ? Sl 02:16 0:01 /usr/bin/prlsga cnetos 6158 0.0 1.4 1026956 28004 ? Sl 02:16 0:00 /usr/libexec/gnome-shell-calendar-server cnetos 6169 0.0 1.4 1120028 27576 ? Sl 02:16 0:00 /usr/libexec/evolution-source-registry root 762 0.0 1.4 327144 26724 ? Ssl 02:09 0:01 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid cnetos 6026 0.0 1.4 1090832 26376 ? Sl 02:16 0:00 /usr/libexec/gnome-settings-daemon [root@CentOS ~]$
按用户 centos 和格式查看所有进程,显示自定义输出 −
[cnetos@CentOS ~]$ ps -u cnetos -o pid,uname,comm PID USER COMMAND 5802 centos gnome-keyring-d 5812 cnetos gnome-session 5819 cnetos dbus-launch 5820 cnetos dbus-daemon 5888 cnetos gvfsd 5893 cnetos gvfsd-fuse 5980 cnetos ssh-agent 5996 cnetos at-spi-bus-laun
pstree 命令
pstree 与 ps 类似,但并不常用。它以更整洁的树形方式显示进程。
[centos@CentOS ~]$ pstree systemd─┬─ModemManager───2*[{ModemManager}] ├─NetworkManager─┬─dhclient │ └─2*[{NetworkManager}] ├─2*[abrt-watch-log] ├─abrtd ├─accounts-daemon───2*[{accounts-daemon}] ├─alsactl ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon} │ └─3*[{at-spi-bus-laun}] ├─at-spi2-registr───2*[{at-spi2-registr}] ├─atd ├─auditd─┬─audispd─┬─sedispatch │ │ └─{audispd} │ └─{auditd} ├─avahi-daemon───avahi-daemon ├─caribou───2*[{caribou}] ├─cgrulesengd ├─chronyd ├─colord───2*[{colord}] ├─crond ├─cupsd
pstree 的总输出可能超过 100 行。通常,ps 会提供更多有用信息。
top 命令
top 是在 Linux 中排除性能问题时最常用的命令之一。它对于 Linux 中的实时统计和进程监控非常有用。以下是从命令行启动 top 时的默认输出。
Tasks: 170 total, 1 running, 169 sleeping, 0 stopped, 0 zombie %Cpu(s): 2.3 us, 2.0 sy, 0.0 ni, 95.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1879668 total, 177020 free, 607544 used, 1095104 buff/cache KiB Swap: 3145724 total, 3145428 free, 296 used. 1034648 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 5404 root 20 0 197832 48024 6744 S 1.3 2.6 1:13.22 Xorg 8013 centos 20 0 555316 23104 13140 S 1.0 1.2 0:14.89 gnome-terminal- 6339 centos 20 0 332336 6016 3248 S 0.3 0.3 0:23.71 prlcc 6351 centos 20 0 21044 1532 1292 S 0.3 0.1 0:02.66 prlshprof
运行 top 时使用的常见 热键(当 top 在 shell 中运行时,按下该键即可访问 热键)。
命令 | 操作 |
---|---|
b | 启用/禁用顶部菜单上的粗体突出显示 |
z | 循环配色方案 |
l | 循环负载平均标题 |
m | 循环内存平均值标题 |
t | 任务信息标题 |
h | 帮助菜单 |
Shift+F | 自定义排序和显示字段 |
以下是 top 的常用命令行开关。
命令 | 操作 |
---|---|
-o | 按列排序(可以添加 - 或 + 以按升序或降序排序)降序排列) |
-u | 仅显示来自指定用户的进程 |
-d | 更新 top 的延迟时间> |
-O | 返回 top 可以应用排序的列的列表 |
top 中的排序选项屏幕,使用 Shift+F 显示。此屏幕允许自定义 top 显示和排序选项。
Fields Management for window 1:Def, whose current sort field is %MEM Navigate with Up/Dn, Right selects for move then <Enter> or Left commits, 'd' or <Space> toggles display, 's' sets sort. Use 'q' or <Esc> to end! * PID = Process Id TGID = Thread Group Id * USER = Effective User Name ENVIRON = Environment vars * PR = Priority vMj = Major Faults delta * NI = Nice Value vMn = Minor Faults delta * VIRT = Virtual Image (KiB) USED = Res+Swap Size (KiB) * RES = Resident Size (KiB) nsIPC = IPC namespace Inode * SHR = Shared Memory (KiB) nsMNT = MNT namespace Inode * S = Process Status nsNET = NET namespace Inode * %CPU = CPU Usage nsPID = PID namespace Inode * %MEM = Memory Usage (RES) nsUSER = USER namespace Inode * TIME+ = CPU Time, hundredths nsUTS = UTS namespace Inode * COMMAND = Command Name/Line PPID = Parent Process pid UID = Effective User Id
top,显示用户 rdc 的进程,并按内存使用情况排序 −
PID USER %MEM PR NI VIRT RES SHR S %CPU TIME+ COMMAND 6130 rdc 6.2 20 0 1349592 117160 33232 S 0.0 1:09.34 gnome-shell 6449 rdc 3.4 20 0 1375872 64428 21400 S 0.0 0:00.43 evolution-calen 6296 rdc 1.7 20 0 1081944 32140 22596 S 0.0 0:00.40 evolution-alarm 6350 rdc 1.6 20 0 560728 29844 4256 S 0.0 0:10.16 prlsga 6281 rdc 1.5 20 0 1027176 28808 17680 S 0.0 0:00.78 nautilus 6158 rdc 1.5 20 0 1026956 28004 19072 S 0.0 0:00.20 gnome-shell-cal
显示有效的顶部字段(精简) −
[centos@CentOS ~]$ top -O PID PPID UID USER RUID RUSER SUID SUSER GID GROUP PGRP TTY TPGID
kill 命令
kill 命令用于通过其 PID 从命令 shell 中终止进程。当 终止 进程时,我们需要指定要发送的信号。该信号让内核知道我们想要如何结束进程。最常用的信号是 −
SIGTERM 是隐含的,因为内核让进程知道它应该尽快停止,因为这样做是安全的。SIGTERM 为进程提供了正常退出并执行安全退出操作的机会。
SIGHUP 大多数守护进程在发送 SIGHUP 时会重新启动。这通常在对配置文件进行更改时用于进程。
SIGKILL 因为 SIGTERM 相当于要求进程关闭。内核需要一个选项来结束不遵守请求的进程。当进程挂起时,SIGKILL 选项用于明确关闭进程。
要查看可以使用 kill 发送的所有信号的列表,可以使用 -l 选项 −
[root@CentOS]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX [root@CentOS rdc]#
使用SIGHUP重新启动系统。
[root@CentOS]# pgrep systemd 1 464 500 643 15071 [root@CentOS]# kill -HUP 1 [root@CentOS]# pgrep systemd 1 464 500 643 15196 15197 15198 [root@CentOS]#
pkill 允许管理员通过进程名称发送 kill 信号。
[root@CentOS]# pgrep ping 19450 [root@CentOS]# pkill -9 ping [root@CentOS]# pgrep ping [root@CentOS]#
killall 将终止所有进程。以 root 身份使用 killall 时要小心,因为它将终止所有用户的所有进程。
[root@CentOS]# killall chrome
free 命令
free 是一个非常简单的命令,通常用于快速检查系统的内存。它显示已使用的物理内存和交换内存的总量。
[root@CentOS]# free total used free shared buff/cache available Mem: 1879668 526284 699796 10304 653588 1141412 Swap: 3145724 0 3145724 [root@CentOS]#
nice 命令
nice 允许管理员根据 CPU 使用率设置进程的调度优先级。nice 基本上是内核如何为进程或作业调度 CPU 时间片。默认情况下,假定进程具有同等的 CPU 资源访问权限。
首先,让我们使用 top 检查当前正在运行的进程的 nice 性。
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 28 root 39 19 0 0 0 S 0.0 0.0 0:00.17 khugepaged 690 root 39 19 16808 1396 1164 S 0.0 0.1 0:00.01 alsactl] 9598 rdc 39 19 980596 21904 10284 S 0.0 1.2 0:00.27 tracker-extract 9599 rdc 39 19 469876 9608 6980 S 0.0 0.5 0:00.04 tracker-miner-a 9609 rdc 39 19 636528 13172 8044 S 0.0 0.7 0:00.12 tracker-miner-f 9611 rdc 39 19 469620 8984 6496 S 0.0 0.5 0:00.02 tracker-miner-u 27 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd 637 rtkit 21 1 164648 1276 1068 S 0.0 0.1 0:00.11 rtkit-daemon 1 root 20 0 128096 6712 3964 S 0.3 0.4 0:03.57 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.50 ksoftirqd/0 7 root 20 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:02.07 rcu_sched
我们希望重点关注 NI 所示的 NICE 列。niceness 范围可以是 -20 到正 19 之间的任意值。-20 表示给定的最高优先级。
nohup nice --20 ping www.google.com &
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 30727 root 0 -20 132108 1640 1264 S 0.0 0.1 0:00.06 ping
renice
renice 允许我们更改已在运行的进程的当前优先级。
renice 17 -p 30727
上述命令将降低我们的 ping 进程命令的优先级。