RHCSA 系列:RHEL 7 中的流程管理:開機、關機和中間的一切 – 第 5 部分

我們將從您按下電源按鈕開啟您的RHEL 7伺服器,直到您在命令列介面中看到登錄畫面,開始對本文進行整體和簡要的修訂。

Linux Boot Process

請注意:

1.相同的基本原則也適用於其他Linux發行版,可能會有輕微修改,以及
2.以下描述並非旨在代表開機過程的詳盡解釋,而僅涉及基本原則。

Linux開機過程

1.電源自檢(POST)初始化並執行硬體檢查。

2.當POST完成後,系統控制權交給第一階段開機載入程式,該程式存儲在硬碟之一的開機區塊(對於使用BIOS和MBR的舊系統)或專用(U)EFI分區中。

3.第一階段開機載入程式然後載入第二階段開機載入程式,通常是GRUB(GRand 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时将受影响的PIDs。例如,在运行之前:

# 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/