Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

systemctl:service 與 chkconfig 的現代替代者

Linux 中有許多命令已存在多年,如今部分已被新命令取代,只是出於習慣,我們往往一時難以適應,例如 ifconfig 之於 ip 命令。

近來在玩 Ubuntu 與 openSUSE 時發現了 systemctl,後來試玩 CentOS 7 時也見到了它。systemctl 是 systemd 體系下的一項工具,查了一下才知道它其實已存在一段時間。它的用途是替代 service 與 chkconfig 兩個命令——儘管個人覺得後者更好用。為順應時代發展,這裡做個整理。

目前許多新的 Linux 發行版,系統對 daemon 的啟動管理已不再採用 SystemV 形式,而是改用 systemd 架構來管理。

一、runlevel 到 target 的轉變

在 systemd 體系中,過去的執行等級(runlevel)概念被新的執行目標(target)取代。target 的命名類似 multi-user.target 這種形式,例如原本的 runlevel 3 對應 multi-user.target,runlevel 5 則相當於 graphical.target。

由於不再使用 runlevel 概念,/etc/inittab 也不再被系統使用——難怪新版 Ubuntu 上找不到 inittab 檔案了。

在 systemd 體系中,預設的 target(相當於過去的預設執行等級)是透過軟連結實現,例如:

ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

在 /lib/systemd/system/ 下定義 runlevelX.target 檔案,主要是為了相容舊有的執行等級管理方式。事實上 /lib/systemd/system/runlevel3.target 同樣被軟連結到 multi-user.target。(註:openSUSE 下位於 /usr/lib/systemd/system/ 目錄。)

二、單元控制(unit)

在 systemd 體系裡,需要管理的 daemon 被稱為單元(unit),對單元的管理透過 systemctl 命令進行。例如顯示目前處於執行狀態的 unit:

#systemctl
#systemctl --all
#systemctl list-units --type=sokect
#systemctl list-units --type=service

註:type 後可接的類型可透過 help 查看 # systemctl -t help。可用類型包含 service、socket、target、device、mount、automount、snapshot、timer、swap、path、slice、scope 等。

三、systemctl 用法與示例

chkconfig、service 與 systemctl 的對照如下:

任務|舊指令|新指令
使某服務自動啟動|chkconfig --level 3 httpd on|systemctl enable httpd.service
使某服務不自動啟動|chkconfig --level 3 httpd off|systemctl disable httpd.service
檢查服務狀態|service httpd status|systemctl status httpd.service(詳細資訊)/systemctl is-active httpd.service(僅顯示是否 Active)
加入自訂服務|chkconfig --add test|systemctl load test.service
刪除服務|chkconfig --del xxx|停掉應用並刪除相應設定檔
顯示所有已啟動服務|chkconfig --list|systemctl list-units --type=service
啟動某服務|service httpd start|systemctl start httpd.service
停止某服務|service httpd stop|systemctl stop httpd.service
重啟某服務|service httpd restart|systemctl restart httpd.service

註:systemctl 後的服務名可到 /usr/lib/systemd/system 目錄查看(openSUSE),其他發行版位於 /lib/systemd/system/ 下。

//openSUSE 下
# systemctl status network.service
network.service - LSB: Configure network interfaces and set up routing
Loaded: loaded (/usr/lib/systemd/system/network.service; enabled)
Active: active (exited) since Mon 2014-09-01 20:05:45 CST; 2h 14min ago
Process: 1022 ExecStart=/etc/init.d/network start (code=exited, status=0/SUCCESS)
Sep 01 20:05:15 linux-pnp8 systemd[1]: Starting LSB: Configure network interfaces and set up routing...
Sep 01 20:05:45 linux-pnp8 systemd[1]: Started LSB: Configure network interfaces and set up routing.

//centos 下
# systemctl status httpd.service
httpd.service - The Apache HTTP Server (prefork MPM)
Loaded: loaded (/lib/systemd/system/httpd.service; disabled)
Active: inactive (dead) <-- 表示未啟動
CGroup: name=systemd:/system/httpd.service

這兩個命令相當於 /etc/init.d/network status 與 /etc/init.d/httpd status,openSUSE 與 CentOS 用法相同,只是顯示路徑不同,其他操作類似。

四、service 設定檔

仍以上面提到的 httpd.service 為例,檔案中可找到如下行:

[Install]
WantedBy=multi-user.target

表示在多使用者目標(multi-user.target,相當於 level 3)時自動啟動。若想讓它在 runlevel 5 下也自啟,可改為:

[Install]
WantedBy=multi-user.target graphical.target

一旦設定了自動啟動(enable),就會在 /etc/systemd/system/.wants/ 下建立一個 httpd.service 的軟連結,連到 /lib/systemd/system/ 下對應的服務。因此要顯示自動啟動狀態的 unit(類似 chkconfig --list 的結果),可執行:

#ls /etc/systemd/system/multi-user.target.wants/

systemctl 就先總結到這裡,它是 systemd 套件內的一項工具,也是該套件最常用的工具。日後再針對 systemd 做一個總結。

Copyright © Linux教程網 All Rights Reserved