RHCSA系列:RHEL 7中的进程管理:启动、关闭和介于其中的一切–第5部分

我们将从按下 电源 按钮打开您的 RHEL 7 服务器到在命令行界面中看到登录屏幕为止的整体和简要修订开始这篇文章。

Linux Boot Process

请注意:

1. 同样的基本原则同样适用于其他 Linux 发行版,可能会有些许修改,并且
2. 以下描述并不意味着对启动过程的详尽解释,而只是基础知识。

Linux 启动过程

1. POST(上电自检)初始化并执行硬件检查。

2.POST 完成后,系统控制被传递到第一阶段引导加载程序,该程序存储在硬盘之一的引导扇区上(对于使用 BIOS 和 MBR 的旧系统),或专用的 (U)EFI 分区上。

3. 然后,第一阶段引导加载程序加载第二阶段引导加载程序,通常是 GRUBGRand Unified Boot Loader),该程序位于 /boot 中,然后加载内核和初始基于 RAM 的文件系统(也称为 initramfs,其中包含执行最终挂载实际根文件系统所需的程序和二进制文件)。

4. 我们会看到一个启动画面,允许我们选择要启动的操作系统和内核:

Boot Menu Screen

5.内核设置系统连接的硬件,一旦根文件系统已挂载,就会启动进程 PID 1,这个进程将初始化其他进程,并显示登录提示符。

注意: 如果我们希望稍后可以这样做,我们可以使用 dmesg 命令 来检查此进程的具体信息,并使用我们在本系列先前文章中解释过的工具来过滤其输出。

Login Screen and Process PID

在上面的示例中,我们使用了众所周知的 ps 命令 来显示当前进程列表,其父进程(或者说启动它们的进程)是 systemd(大多数现代 Linux 发行版已经切换到的系统和服务管理器)在系统启动期间:

# ps -o ppid,pid,uname,comm --ppid=1

记住 -o 标志(代表 –format)允许您以定制格式呈现 ps 的输出,以满足您的需求,使用 STANDARD FORMAT SPECIFIERS 部分中 man ps 中指定的关键字。

另一个您需要定义 ps 输出而不是使用默认输出的情况是当您需要查找导致显著 CPU 和/或内存负载的进程,并据此对它们进行排序时:

# ps aux --sort=+pcpu              # Sort by %CPU (ascending)
# ps aux --sort=-pcpu              # Sort by %CPU (descending)
# ps aux --sort=+pmem              # Sort by %MEM (ascending)
# ps aux --sort=-pmem              # Sort by %MEM (descending)
# ps aux --sort=+pcpu,-pmem        # Combine sort by %CPU (ascending) and %MEM (descending)
Customize ps Command Output

SystemD 简介

Linux世界中很少有决定像主要Linux发行版采用systemd那样引起争议。Systemd的支持者将其主要优势列为:

阅读更多: ‘init’和‘systemd’背后的故事

1. Systemd允许在系统启动期间并行进行更多处理(与较旧的SysVinit相反,后者始终倾向于更慢,因为它逐个启动进程,检查一个是否依赖于另一个,然后等待守护程序启动,以便启动更多服务),以及

2. 它作为运行系统中的动态资源管理。因此,服务会在需要时启动(以避免在启动期间无效地启动服务而占用系统资源),而不是在引导过程中没有有效原因的情况下启动。

3.SysVinit脚本的向后兼容性。

Systemdsystemctl实用程序控制。如果您来自SysVinit背景,您可能熟悉:

  1. 在那些较旧的系统中用于管理SysVinit脚本的service工具,以及
  2. 用于更新和查询系统服务运行级别信息的chkconfig实用程序。
  3. 关机,你肯定已经使用过好几次来重新启动或停止运行中的系统。

以下表格显示了这些传统工具与systemctl的使用方式之间的相似之处:

Legacy tool Systemctl equivalent Description
service name start systemctl start name Start name (where name is a service)
service name stop systemctl stop name Stop name
service name condrestart systemctl try-restart name Restarts name (if it’s already running)
service name restart systemctl restart name Restarts name
service name reload systemctl reload name Reloads the configuration for name
service name status systemctl status name Displays the current status of name
service –status-all systemctl Displays the status of all current services
chkconfig name on systemctl enable name Enable name to run on startup as specified in the unit file (the file to which the symlink points). The process of enabling or disabling a service to start automatically on boot consists in adding or removing symbolic links inside the /etc/systemd/system directory.
chkconfig name off systemctl disable name Disables name to run on startup as specified in the unit file (the file to which the symlink points)
chkconfig –list name systemctl is-enabled name Verify whether name (a specific service) is currently enabled
chkconfig –list systemctl –type=service Displays all services and tells whether they are enabled or disabled
shutdown -h now systemctl poweroff Power-off the machine (halt)
shutdown -r now systemctl reboot Reboot the system

Systemd还引入了单位(可以是服务、挂载点、设备或网络套接字)和目标的概念(这是systemd管理同时启动多个相关进程的方式,并且可以被视为等同于SysVinit系统中的运行级别的等效物,虽然并不完全相同。

总结

与进程管理相关的其他任务还包括但可能不限于以下功能:

1. 调整进程的执行优先级,涉及系统资源的使用:

这是通过renice实用程序实现的,它改变了一个或多个运行进程的调度优先级。简单来说,调度优先级是一个功能,允许内核(在版本=> 2.6中存在)根据给定进程的分配执行优先级(也称为niceness,在-2019的范围内)来分配系统资源。

renice的基本语法如下:

# renice [-n] priority [-gpu] identifier

在上述通用命令中,第一个参数是要使用的优先级值,而另一个参数可以被解释为进程IDs(这是默认设置),进程组IDs,用户IDs或用户名。普通用户(除root外)只能修改他或她拥有的进程的调度优先级,并且只能增加niceness级别(这意味着占用更少的系统资源)。

Process Scheduling Priority
2. 根据需要终止(或中断正常执行)进程:

更准确地说,终止进程意味着向其发送信号,要么使其正常结束执行(SIGTERM=15),要么立即结束执行(SIGKILL=9)通过kill或pkill命令

这两个工具之间的区别在于前者用于终止特定进程或整个进程组,而后者允许您根据名称和其他属性执行相同操作。

此外,pkillpgrep捆绑在一起,它会显示应用pkill将受影响的PID。例如,在运行之前:

# pkill -u gacanepa

查看一下由gacanepa拥有的PIDs可能是有用的:

# pgrep -l -u gacanepa
Find PIDs of User

默认情况下,killpkill都会向进程发送SIGTERM信号。正如我们之前提到的,这个信号可以被忽略(直到进程完成执行或永久忽略),所以当你需要以合法理由停止运行中的进程时,你需要在命令行中指定SIGKILL信号:

# kill -9 identifier               # Kill a process or a process group
# kill -s SIGNAL identifier        # Idem
# pkill -s SIGNAL identifier       # Kill a process by name or other attributes 

结论

在本文中,我们解释了启动过程在一个RHEL 7系统中的基础知识,并分析了一些可用的工具,以帮助您使用常见实用程序和systemd特定命令来管理进程。

请注意,这个列表并不意味着覆盖这个主题的所有细节,所以请随时使用下面的评论表单添加您自己喜欢的工具和命令到这篇文章中。欢迎提问和其他评论。

Source:
https://www.tecmint.com/rhcsa-exam-boot-process-and-process-management/