1.1 什麼是systemd
Systemd(System Daemon)是CentOS7系統中的系統管理守護程序、工具和庫的集合,用于取代早期的init程序。Systemd的功能是用于集中管理和配置Linux系統。
systemd是CentOS7系統中啟動的第一個程序(PID等于1),其他所有程序都是他的子程序。
C6:服務啟動方式
# /etc/init.d/network restart
Restarting network (via systemctl): [ OK ]
# service network restart
Restarting network (via systemctl): [ 确定 ]
1.2 systemd相關路徑檔案
路徑 | 描述 |
/usr/lib/systemd/system | 啟動服務程式所在的路徑 |
/etc/systemd/system | 不同運作級别啟動檔案路徑 |
/etc/systemd/system/multi-user.target.wants | 開機自啟檔案所在路徑 |
1.3 systemd管理服務相關指令
1)常用指令
systemctl指令 | 作用 |
start | 啟動服務 |
stop | 停止服務 |
restart | 重新開機服務 |
reload | 重新加載配置 |
status | 檢視服務運作狀态 |
2)使用執行個體:
# 啟動crond服務
systemctl start crond
# 停止crond服務
systemctl stop crond
# 重新開機crond服務
systemctl restart crond
# 重新加載配置crond
systemctl reload crond
# 檢視crond服務運作狀态
systemctl status crond
3)運作狀态描述
狀态 | 描述 |
loaded | 服務單元的配置檔案已經被處理 |
active(running) | 服務持續運作 |
inactive (dead) | 服務停止狀态 |
enabled | 服務設定為開機運作 |
disabled | 服務設定為開機不運作 |
1.4 systemd服務開機自啟
1)常用指令
systemctl指令 | 作用 |
enable | 開機自動啟動 |
disable | 開機不自動啟動 |
2)手工設定開機自啟動
在 /etc/systemd/system/multi-user.target.wants/ 目錄下設定快捷方式
# ln -s /usr/lib/systemd/system/crond.service /etc/systemd/system/multi-user.target.wants/crond.service
# ls -l /etc/systemd/system/multi-user.target.wants/|grep crond
# systemctl status crond
(0基礎學Linux系列)2.15 Linux服務管理 3)手工設定開機停止啟動
# rm -f /etc/systemd/system/multi-user.target.wants/crond.service
# ls -l /etc/systemd/system/multi-user.target.wants/|grep crond
# systemctl status crond
1.5 linux伺服器 開機自啟動服務優化
1)需要保留的服務
服務名稱 | 服務作用 |
rsyslog.service | 日志系統 |
sshd.service | 遠端連接配接SSHD |
sysstat.service | 性能監控 |
network | 網絡服務(使用chkconfig開啟) |
2)處理方法
# systemctl list-unit-files|egrep -v "network.ta|rsyslog|sshd\.|sysstat|static"|awk '{print "systemctl disable "$1}'|bash
# systemctl list-unit-files|grep enabled
網絡服務的開機自啟動
# chkconfig network on
# chkconfig --list|grep network