Linux重啟(qi)命令介(jie)紹
時間:2018-12-29 來源:華清遠見
下面介(jie)紹(shao)在 Linux 操作系統中重啟和關閉相關的命令(ling):shutdown、reboot、init、halt、poweroff、systemctl,你可以(yi)根據需要來選擇適合的 Linux 命令(ling)關閉或重新啟動系統。其(qi)中 shutdown、halt、poweroff、reboot 命令(ling)是(shi)用來停機、重啟或切(qie)斷電(dian)源, systemctl 命令(ling)管(guan)(guan)理 systemd,是(shi) Linux 系統和服務器的管(guan)(guan)理程(cheng)序。
使(shi)用 shutdown 命令關閉和重啟 Linux 系統
shutdown 命令用于斷電或重啟本地和遠程的(de)(de) Linux 機器。它為高效(xiao)完成作業提供多個(ge)選(xuan)項(xiang)。如果使用了(le)時間參數,系統關(guan)閉的(de)(de) 5 分鐘之前,會創(chuang)建 /run/nologin 文(wen)件,以(yi)確保后續的(de)(de)登(deng)錄會被(bei)拒(ju)絕。
通(tong)用語法如下(xia):
# shutdown [OPTION] [TIME] [MESSAGE]
運行(xing)下面的命令來立(li)(li)即(ji)關(guan)閉 Linux 機器(qi)。它會立(li)(li)刻殺死所(suo)有進(jin)程,并關(guan)閉系(xi)統:
# shutdown -h now
-h:如果不特指 -halt 選項,這等價(jia)于 -poweroff 選項。
另外我們(men)可(ke)以使用(yong)帶(dai)有 -halt 選(xuan)項(xiang)的 shutdown 命令來立即關閉設(she)備(bei):
# shutdown --halt now
或者:
# shutdown -H now
-H, --halt:停止設備運(yun)行。
另外我們可(ke)以使用帶有 poweroff 選項的(de) shutdown 命令來立即關閉設備:
# shutdown --poweroff now
或者:
# shutdown -P now
-P, --poweroff:切(qie)斷電源(默認)。
如果您(nin)沒有使用(yong)時(shi)間選(xuan)項運行下面的命令,它將會在一分鐘(zhong)后執(zhi)行給(gei)出的命令:
# shutdown -h
Shutdown scheduled for THU 2018-11-01 06:42:31 EDT, use 'shutdown -c' to cancel.
其他的(de)登錄用戶都能在中(zhong)斷(duan)中(zhong)看到(dao)如下的(de)廣播消(xiao)息:
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!
對于使(shi)用了 -halt 選項:
# shutdown -H
Shutdown scheduled for THU 2018-11-01 06:37:53 EDT, use 'shutdown -c' to cancel.
對于使用(yong)了 -poweroff 選項:
# shutdown -P
Shutdown scheduled for THU 2018-11-01 06:40:07 EDT, use 'shutdown -c' to cancel.
可以在您的(de)終端上敲擊 shutdown -c 選項取消(xiao)操作:
# shutdown -c
其他的登錄用戶都能在中斷中看到如下的廣播消息:
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!
添加時間參數,如果(guo)你想(xiang)在(zai) N 秒之后(hou)執行關閉或(huo)重啟(qi)操作。這里,您可以(yi)為所有登(deng)錄用(yong)戶(hu)添加自定義廣播(bo)消息。例如,我(wo)們將在(zai)五分鐘后(hou)重啟(qi)設備:
# shutdown -r +5 "To activate the latest Kernel"
Shutdown scheduled for THU 2018-11-01 07:13:16 EDT, use 'shutdown -c' to cancel.
運行下面的命(ming)令立即(ji)重啟 Linux 機(ji)器。它(ta)會立即(ji)殺死所有進程并且重新(xin)啟動系統:
# shutdown -r now
-r, --reboot: 重啟設(she)備。
通過 reboot 命令關(guan)閉(bi)和重啟 Linux 系統(tong)
reboot 命(ming)(ming)令用(yong)于關閉和重(zhong)啟本地(di)或遠程設備。reboot 命(ming)(ming)令擁有兩個(ge)實用(yong)的選項。
它能夠優雅的關閉和(he)重啟設備(就好像在系統菜(cai)單(dan)中(zhong)惦(dian)記(ji)重啟選(xuan)項(xiang)一樣(yang)簡(jian)單(dan))。
執行不(bu)帶任何參(can)數的 reboot 命令來重(zhong)啟 Linux 機器:
# reboot
執行(xing)帶 -p 參數的 reboot 命令來關閉 Linux 機器電源:
# reboot -p
-p, --poweroff:調(diao)用 halt 或 poweroff 命令,切斷設備電源(yuan)。
執行帶 -f 參數的 reboot 命(ming)令來強制重啟 Linux 設備(這類似按(an)壓機器上的電源鍵):
# reboot -f
-f, --force:立刻強制中斷(duan),切(qie)斷(duan)電(dian)源或(huo)重啟。
通過(guo) init 命令(ling)關閉(bi)和重啟 Linux 系統
init(“initialization” 的簡寫)是系統(tong)啟動(dong)的第(di)一(yi)個進程。
它(ta)將會(hui)檢(jian)查 /etc/inittab 文件并決定(ding) linux 運行級(ji)別。同時,允許(xu)用戶在(zai)(zai) Linux 設(she)備(bei)上(shang)執(zhi)行關機或重啟操作. 這里存(cun)在(zai)(zai)從 0 到(dao) 6 的七個運行等級(ji)。
執行以下 init 命(ming)令關閉系統:
# init 0
0: 停機 – 關閉(bi)系統。
運行(xing)下面的 init 命令重啟設備:
# init 6
6:重啟(qi) – 重啟(qi)設備。
通過 halt 命令關閉和重啟(qi) Linux 系(xi)統
halt 命(ming)令用來切斷(duan)電源或(huo)關閉遠程 Linux 機器或(huo)本地主機。中斷(duan)所有進(jin)程并關閉 cpu:
# halt
通(tong)過 poweroff 命令(ling)關(guan)閉和重啟 Linux 系統
poweroff 命令用來切(qie)斷電源(yuan)(yuan)或關閉遠程 Linux 機器(qi)或本地主(zhu)機。poweroff 很像 halt,但是它可以關閉設備硬件(燈和其他 PC 上的其它東西)。它會給(gei)主(zhu)板(ban)發送(song) ACPI 指令,然后信號發送(song)到電源(yuan)(yuan),切(qie)斷電源(yuan)(yuan)。
# poweroff
通(tong)過 systemctl 命(ming)令關閉和重啟 Linux 系統
systemd 是一款適用(yong)于所有主(zhu)流 Linux 發型(xing)版(ban)的(de)全新 init 系統和系統管理器,而(er)不(bu)是傳(chuan)統的(de) SysV init 系統。
systemd 兼容(rong)與(yu) SysV 和(he) LSB 初始化腳本。它能夠替代 SysV init 系統(tong)。systemd 是內核(he)啟動的第一(yi)個進(jin)程,并持有序號為 1 的進(jin)程 PID。
它是一(yi)切進程的父進程,Fedora 15 是第(di)一(yi)個適配安裝 systemd (替代了 upstart)的發行版。
systemctl 是命(ming)令行下管理 systemd 守護進程和服務的主要工(gong)具(如 start、restart、stop、enable、disable、reload & status)。
systemd 使用(yong)(yong) .service 文件(jian)而不是 SysV init 使用(yong)(yong)的(de)(de) bash 腳本。 systemd 將所有守護進程歸與自(zi)身的(de)(de) Linux cgroups 用(yong)(yong)戶組下(xia),您可以瀏覽 /cgroup/systemd 文件(jian)查(cha)看該系統層(ceng)次等級:
# systemctl halt
# systemctl poweroff
# systemctl reboot
# systemctl suspend
# systemctl hibernate
結語
shutdown、reboot、init、halt、poweroff、systemctl 命令(ling)可(ke)以(yi)達到重啟(qi)(qi)和(he)關閉(bi) Linux 系統(tong)目的(de),都是在命令(ling)行(xing)下操作。如果你對命令(ling)行(xing)比(bi)較熟(shu)悉,那這樣做是最便捷的(de),也是最安全的(de)。同時重啟(qi)(qi)和(he)關閉(bi)系統(tong)的(de)命令(ling)不多(duo),你可(ke)以(yi)選擇其中拿手的(de)來完(wan)成任務(wu)。