初次體驗CentOS 7的systemd

新發布的CentOS 7 中使用systemd服務代替了之前版本的SysV服務,對比下兩種啓動方式的不同。


修改系統啓動級別

舊版

編輯配置文件/etc/inittab,設置啓動級別爲3 (多用戶文字界面),修改initdefault前面的數字爲3,保存重啓

新版

修改默認啓動級別爲3

systemctl enable multi-user.target

這個命令實際則是在目錄 /etc/systemd/system 下創建了一個軟鏈接

ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'

若修改默認啓動級別爲5,需要將之前的啓動級別disable

systemctl disable multi-user.target

該命令實際刪除了軟鏈接default.target

rm '/etc/systemd/system/default.target'

然後再啓用啓動級別5

systemctl enable graphical.target

實際創建了新的軟鏈接

ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'

當然你也可以跳過命令直接以創建軟鏈接的方式來改變啓動級別


應用程序的自啓動項

當通過yum安裝了httpd服務後,準備將其添加到自啓動項裏,

舊版

chkconfig httpd on

新版

systemctl enable httpd

實際是創建了軟鏈接

ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

關掉httpd的自啓動

systemctl disable httpd

實際軟鏈接被刪除

rm '/etc/systemd/system/multi-user.target.wants/httpd.service'

關於啓動級別3下面的服務啓動項都在/etc/systemd/system/multi-user.target.wants目錄下,而級別5的則在目錄graphical.target.wants下面

我們打開這個鏈接文件 httpd.service 可以看到內容,就是有關httpd的啓動腳本

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target


關於服務的啓動/關閉/重啓

舊版

service httpd {start|stop|restart}

新版

systemctl {start|stop|restart} httpd

查看當前httpd的運行狀態

systemctl status httpd

輸出結果

httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Thu 2014-07-17 15:12:50 CST; 4s ago
  Process: 2762 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 2769 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ?..2769 /usr/sbin/httpd -DFOREGROUND
           ?..2770 /usr/sbin/httpd -DFOREGROUND
           ?..2771 /usr/sbin/httpd -DFOREGROUND
           ?..2772 /usr/sbin/httpd -DFOREGROUND
           ?..2773 /usr/sbin/httpd -DFOREGROUND
           ?..2774 /usr/sbin/httpd -DFOREGROUND
Jul 17 15:12:50 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Jul 17 15:12:50 localhost.localdomain httpd[2769]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the ...his message
Jul 17 15:12:50 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full

查看所有服務自啓動狀態

舊版

chkconfig --list

新版

systemctl list-unit-files


以上是我對systemd的一個初步印象,需要一個逐步適應的過程,希望大家多多討論交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章