新發布的CentOS 7 中使用systemd服務代替了以前版本的SysV服務,對比下兩種啓動方式的不一樣。bash
修改系統啓動級別less
舊版
dom
編輯配置文件/etc/inittab,設置啓動級別爲3 (多用戶文字界面),修改initdefault前面的數字爲3,保存重啓ide
新版spa
修改默認啓動級別爲3rest
systemctl enable multi-user.target
這個命令實際則是在目錄 /etc/systemd/system 下建立了一個軟連接code
ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'
若修改默認啓動級別爲5,須要將以前的啓動級別disableorm
systemctl disable multi-user.target
該命令實際刪除了軟連接default.targetserver
rm '/etc/systemd/system/default.target'
而後再啓用啓動級別5ip
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的一個初步印象,須要一個逐步適應的過程,但願你們多多討論交流。