當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > Linux重啟命令介紹
下面介紹在 Linux 操作系統(tǒng)中重啟和關(guān)閉相關(guān)的命令:shutdown、reboot、init、halt、poweroff、systemctl,你可以根據(jù)需要來(lái)選擇適合的 Linux 命令關(guān)閉或重新啟動(dòng)系統(tǒng)。其中 shutdown、halt、poweroff、reboot 命令是用來(lái)停機(jī)、重啟或切斷電源, systemctl 命令管理 systemd,是 Linux 系統(tǒng)和服務(wù)器的管理程序。
使用 shutdown 命令關(guān)閉和重啟 Linux 系統(tǒng)
shutdown 命令用于斷電或重啟本地和遠(yuǎn)程的 Linux 機(jī)器。它為高效完成作業(yè)提供多個(gè)選項(xiàng)。如果使用了時(shí)間參數(shù),系統(tǒng)關(guān)閉的 5 分鐘之前,會(huì)創(chuàng)建 /run/nologin 文件,以確保后續(xù)的登錄會(huì)被拒絕。
通用語(yǔ)法如下:
# shutdown [OPTION] [TIME] [MESSAGE]
運(yùn)行下面的命令來(lái)立即關(guān)閉 Linux 機(jī)器。它會(huì)立刻殺死所有進(jìn)程,并關(guān)閉系統(tǒng):
# shutdown -h now
-h:如果不特指 -halt 選項(xiàng),這等價(jià)于 -poweroff 選項(xiàng)。
另外我們可以使用帶有 -halt 選項(xiàng)的 shutdown 命令來(lái)立即關(guān)閉設(shè)備:
# shutdown --halt now
或者:
# shutdown -H now
-H, --halt:停止設(shè)備運(yùn)行。
另外我們可以使用帶有 poweroff 選項(xiàng)的 shutdown 命令來(lái)立即關(guān)閉設(shè)備:
# shutdown --poweroff now
或者:
# shutdown -P now
-P, --poweroff:切斷電源(默認(rèn))。
如果您沒(méi)有使用時(shí)間選項(xiàng)運(yùn)行下面的命令,它將會(huì)在一分鐘后執(zhí)行給出的命令:
# shutdown -h
Shutdown scheduled for THU 2018-11-01 06:42:31 EDT, use 'shutdown -c' to cancel.
其他的登錄用戶(hù)都能在中斷中看到如下的廣播消息:
Broadcast message from root@2daygeek.com (THU 2018-11-01 06:41:31 EDT):
The system is going down for power-off at THU 2018-11-01 06:42:31 EDT!
對(duì)于使用了 -halt 選項(xiàng):
# shutdown -H
Shutdown scheduled for THU 2018-11-01 06:37:53 EDT, use 'shutdown -c' to cancel.
對(duì)于使用了 -poweroff 選項(xiàng):
# shutdown -P
Shutdown scheduled for THU 2018-11-01 06:40:07 EDT, use 'shutdown -c' to cancel.
可以在您的終端上敲擊 shutdown -c 選項(xiàng)取消操作:
# shutdown -c
其他的登錄用戶(hù)都能在中斷中看到如下的廣播消息:
Broadcast message from root@vps.2daygeek.com (THU 2018-11-01 06:41:35 EDT):
The system shutdown has been cancelled at THU 2018-11-01 06:42:35 EDT!
添加時(shí)間參數(shù),如果你想在 N 秒之后執(zhí)行關(guān)閉或重啟操作。這里,您可以為所有登錄用戶(hù)添加自定義廣播消息。例如,我們將在五分鐘后重啟設(shè)備:
# shutdown -r +5 "To activate the latest Kernel"
Shutdown scheduled for THU 2018-11-01 07:13:16 EDT, use 'shutdown -c' to cancel.
運(yùn)行下面的命令立即重啟 Linux 機(jī)器。它會(huì)立即殺死所有進(jìn)程并且重新啟動(dòng)系統(tǒng):
# shutdown -r now
-r, --reboot: 重啟設(shè)備。
通過(guò) reboot 命令關(guān)閉和重啟 Linux 系統(tǒng)
reboot 命令用于關(guān)閉和重啟本地或遠(yuǎn)程設(shè)備。reboot 命令擁有兩個(gè)實(shí)用的選項(xiàng)。
它能夠優(yōu)雅的關(guān)閉和重啟設(shè)備(就好像在系統(tǒng)菜單中惦記重啟選項(xiàng)一樣簡(jiǎn)單)。
執(zhí)行不帶任何參數(shù)的 reboot 命令來(lái)重啟 Linux 機(jī)器:
# reboot
執(zhí)行帶 -p 參數(shù)的 reboot 命令來(lái)關(guān)閉 Linux 機(jī)器電源:
# reboot -p
-p, --poweroff:調(diào)用 halt 或 poweroff 命令,切斷設(shè)備電源。
執(zhí)行帶 -f 參數(shù)的 reboot 命令來(lái)強(qiáng)制重啟 Linux 設(shè)備(這類(lèi)似按壓機(jī)器上的電源鍵):
# reboot -f
-f, --force:立刻強(qiáng)制中斷,切斷電源或重啟。
通過(guò) init 命令關(guān)閉和重啟 Linux 系統(tǒng)
init(“initialization” 的簡(jiǎn)寫(xiě))是系統(tǒng)啟動(dòng)的第一個(gè)進(jìn)程。
它將會(huì)檢查 /etc/inittab 文件并決定 linux 運(yùn)行級(jí)別。同時(shí),允許用戶(hù)在 Linux 設(shè)備上執(zhí)行關(guān)機(jī)或重啟操作. 這里存在從 0 到 6 的七個(gè)運(yùn)行等級(jí)。
執(zhí)行以下 init 命令關(guān)閉系統(tǒng):
# init 0
0: 停機(jī) – 關(guān)閉系統(tǒng)。
運(yùn)行下面的 init 命令重啟設(shè)備:
# init 6
6:重啟 – 重啟設(shè)備。
通過(guò) halt 命令關(guān)閉和重啟 Linux 系統(tǒng)
halt 命令用來(lái)切斷電源或關(guān)閉遠(yuǎn)程 Linux 機(jī)器或本地主機(jī)。中斷所有進(jìn)程并關(guān)閉 cpu:
# halt
通過(guò) poweroff 命令關(guān)閉和重啟 Linux 系統(tǒng)
poweroff 命令用來(lái)切斷電源或關(guān)閉遠(yuǎn)程 Linux 機(jī)器或本地主機(jī)。poweroff 很像 halt,但是它可以關(guān)閉設(shè)備硬件(燈和其他 PC 上的其它東西)。它會(huì)給主板發(fā)送 ACPI 指令,然后信號(hào)發(fā)送到電源,切斷電源。
# poweroff
通過(guò) systemctl 命令關(guān)閉和重啟 Linux 系統(tǒng)
systemd 是一款適用于所有主流 Linux 發(fā)型版的全新 init 系統(tǒng)和系統(tǒng)管理器,而不是傳統(tǒng)的 SysV init 系統(tǒng)。
systemd 兼容與 SysV 和 LSB 初始化腳本。它能夠替代 SysV init 系統(tǒng)。systemd 是內(nèi)核啟動(dòng)的第一個(gè)進(jìn)程,并持有序號(hào)為 1 的進(jìn)程 PID。
它是一切進(jìn)程的父進(jìn)程,F(xiàn)edora 15 是第一個(gè)適配安裝 systemd (替代了 upstart)的發(fā)行版。
systemctl 是命令行下管理 systemd 守護(hù)進(jìn)程和服務(wù)的主要工具(如 start、restart、stop、enable、disable、reload & status)。
systemd 使用 .service 文件而不是 SysV init 使用的 bash 腳本。 systemd 將所有守護(hù)進(jìn)程歸與自身的 Linux cgroups 用戶(hù)組下,您可以瀏覽 /cgroup/systemd 文件查看該系統(tǒng)層次等級(jí):
# systemctl halt
# systemctl poweroff
# systemctl reboot
# systemctl suspend
# systemctl hibernate
結(jié)語(yǔ)
shutdown、reboot、init、halt、poweroff、systemctl 命令可以達(dá)到重啟和關(guān)閉 Linux 系統(tǒng)目的,都是在命令行下操作。如果你對(duì)命令行比較熟悉,那這樣做是最便捷的,也是最安全的。同時(shí)重啟和關(guān)閉系統(tǒng)的命令不多,你可以選擇其中拿手的來(lái)完成任務(wù)。