📜  Linux管理员-流程管理

📅  最后修改于: 2020-10-31 13:29:06             🧑  作者: Mango


以下是与流程管理一起使用的常见命令-bg,fg,nohup,ps,pstree,top,kill,killall,免费,正常运行时间,很好。

处理流程

快速说明:在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 – Bringsthe过程中对前景

  • BG – Movesthe过程的背景

  • jobs-附加到外壳程序的当前进程列表

  • ctrl + z -Ctrl + z组合键可休眠当前进程

  • −在后台启动进程

让我们开始使用shell命令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

如果继续进行,您会发现前台作业卡在了外壳中。现在,让我们进入休眠状态,然后在后台重新启用它。

  • 命中控制+ Z
  • 键入:bg 1,将第一个作业发送到后台并启动它。
[root@CentOS ~]$ fg 1 
sleep 20 
^Z 
[1]+  Stopped                 sleep 20

[root@CentOS ~]$ bg 1 
[1]+ sleep 20 &

[root@CentOS ~]$

Nohup

在外壳程序或终端上工作时,值得注意的是,默认情况下,关闭外壳程序或用户注销时,连接到外壳程序的所有进程和作业都会终止。使用nohup时,如果用户注销或关闭该进程所连接的外壳,该进程将继续运行。

[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一起使用的最常见的命令行开关。

Switch Action
a Excludes constraints of only the reporting processes for the current user
x Shows processes not attached to a tty or shell
w Formats wide output display of the output
e Shows environment after the command
-e Selects all processes
-o User-defined formatted output
-u Shows all processes by a specific user
-C Shows all processes by name or process id
–sort Sorts the processes by definition

查看无人用户正在使用的所有进程-

[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 ~]$

按用户中心和格式查看所有进程,显示定制输出-

[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命令

pstreeps类似,但不经常使用。它以整洁的树状显示过程。

[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将提供更多有用的信息。

最高指挥官

在对Linux中的性能问题进行故障排除时, top是最常用的命令之一。它对于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在外壳中运行时,通过按此可以访问热键)。

Command Action
b Enables / disables bold highlighting on top menu
z Cycles the color scheme
l Cycles the load average heading
m Cycles the memory average heading
t Task information heading
h Help menu
Shift+F Customizes sorting and display fields

以下是top的常见命令行开关。

Command Action
-o Sorts by column (can prepend with – or + to sort ascending or descending)
-u Shows only processes from a specified user
-d Updates the delay time of top
-O Returns a list of columns which top can apply sorting

排序选项屏幕位于顶部,使用Shift + F呈现。此屏幕允许自定义顶部显示和排序选项。

Fields Management for window 1:Def, whose current sort field is %MEM 
Navigate with Up/Dn, Right selects for move then  or Left commits, 
 'd' or  toggles display, 's' sets sort.  Use 'q' or  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

顶部,显示用户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命令用于通过命令外壳的PID终止命令外壳中的进程。当杀死进程时,我们需要指定要发送的信号。该信号使内核知道我们要如何结束进程。最常用的信号是-

  • 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将允许管理员按进程名称发送终止信号。

[root@CentOS]# pgrep ping 
19450 
[root@CentOS]# pkill -9 ping 
[root@CentOS]# pgrep ping 
[root@CentOS]#

killall将杀死所有进程。使用killall作为root时要小心,因为它将杀死所有用户的所有进程。

[root@CentOS]# killall chrome

自由命令

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将允许管理员根据CPU使用率设置进程的调度优先级。基本上,内核的好处是内核将如何为进程或作业调度CPU时间片。默认情况下,假定该进程被授予对CPU资源的平等访问权限。

首先,让我们使用top检查当前正在运行的进程的美观程度。

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列。尼斯范围可以介于-20到正19之间。-20表示最高的给定优先级。

nohup nice --20 ping www.google.com &

伦尼斯

renice允许我们更改已经运行的进程的当前优先级。

renice 17 -p 30727

上面的命令将降低我们ping处理命令的优先级。