最近在玩ubuntu和opensuse時發現了systemctl命令了,後來在試玩centos7時也發現了該命令,systemctl是systemd下的一個工具。網上查了下,該命令已經存在很久了。該命令是用來替代service和chkconfig兩個命令的 --- 儘管個人感覺還是後者好用。
為了順應時間的發展,這裡總結下。在目前很多linux的新發行版本裡,系統對於daemon的啟動管理方法不再採用SystemV形式,而是使用了sytemd的架構來管理daemon的啟動。
一、runlevel 到 target的改變
在systemd的管理體系裡面,以前的運行層級(runlevel)的概念被新的運行目標(target)所取代。tartget的命名類似於multi-user.target等這種形式,比如原來的運行層級3(runlevel3)就對應新的多使用者目標(multi-user.target),run level 5就相當於graphical.target。
由於不再使用runlevle概念,所以/etc/inittab也不再被系統使用 --- 無怪乎在新版本ubuntu上找不到inittab檔案了。
而在systemd的管理體系裡面,預設的target(相當於以前的預設運行層級)是通過軟鏈來實現。如:
ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target
在/lib/systemd/system/ 下面定義runlevelX.target檔案目的主要是為了能夠相容以前的運行層級level的管理方法。 事實上/lib/systemd/system/runlevel3.target,同樣是被軟串連到multi-user.target。
註:opensuse下是在/usr/lib/systemd/system/目錄下。
二、單元控制(unit)
在systemd管理體系裡,稱呼需要管理的daemon為單元(unit)。對於單元(unit)的管理是通過命令systemctl來進行控制的。例如顯示當前的處於運行狀態的unit(即daemon),如:
#systemctl
#systemctl --all
#systemctl list-units --type=sokect
#systemctl list-units --type=service
注:type後面可以接的類型可以通過help查看
361way:~ # systemctl -t help
Available unit types:
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下
361way:~ # 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:15 linux-pnp8 network[1022]: Setting up network interfaces:
Sep 01 20:05:15 linux-pnp8 network[1022]: lo
Sep 01 20:05:15 linux-pnp8 network[1022]: lo IP address: 127.0.0.1/8
Sep 01 20:05:45 linux-pnp8 network[1022]: ..done..done..doneSetting up service network . . . . . . . . . . . . ...done
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配置為例,httpd.service檔案裡可以找到如下行:
[Install]
WantedBy=multi-user.target
則表明在多使用者目標(multi-user.target,相當於level3)時自動啟動。如果想在runlevel 5下也自啟動,則可以將配置改為如下:
[Install]
WantedBy=multi-user.target graphical.target
一旦設定了自動啟動(enbale),就在/etc/systemd/system/.wants/下面建了一個httpd.service的軟串連,串連到/lib/systemd/system/下的相應服務那裡 。所以顯示自動啟動狀態的unit (類似於chekconfig --list命令的結果),可以通過下面的方法:
#ls /etc/systemd/system/multi-user.target.wants/
systemctl的總結先寫到這裡,其是systemd包內的一個工具,也是該包中最為常用的工具。回頭再針對systemd做一個總結。