在目前很多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 --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包內的一個工具,也是該包中最爲常用的工具。
任務 | 舊指令 | 新指令 |
使某服務自動啓動 | 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 --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 |