1. 程式人生 > >[svc]centos7的服務治理-systemd

[svc]centos7的服務治理-systemd

pat daemon nts mis com dockerd link 硬件 ner

經常用到的高頻命令小結

- 所有服務unit放在這裏
ll /usr/lib/systemd/system

- 默認啟動級別
[root@n1 ~]# ll /etc/systemd/system/default.target 
lrwxrwxrwx 1 root root 41 Mar  4 09:02 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target

- 開機啟動的服務
ll /etc/systemd/system/multi-user.target.wants/

- 哪些服務開機會啟動
[root@n1 ~]# systemctl list-unit-files --type service |grep enable
[email protected]                               enabled 
crond.service                                 enabled 
docker.service                                enabled 
[email protected]                                enabled 
ntpdate.service                               enabled 
rsyslog.service                               enabled 
sshd.service                                  enabled 
sysstat.service                               enabled 

- 目前哪些服務處於active狀態
[root@n1 ~]# systemctl list-units --type service |grep active
crond.service                      loaded active running Command Scheduler
dbus.service                       loaded active running D-Bus System Message Bus
docker.service                     loaded active running Docker Application Container Engine
[email protected]                 loaded active running Getty on tty1

- 查看某個服務是否開機自啟
systemctl is-enabled httpd.service 或
systemctl status httpd.service

systemd 有很多unit,其中service.unit是管理系統服務的.
技術分享圖片

unit的類型

/usr/lib/systemd/system

技術分享圖片

查看管理系統服務的相關的, 即 .service後綴的

[root@n1 system]# ls /usr/lib/systemd/system/*.service|more
/usr/lib/systemd/system/arp-ethers.service
/usr/lib/systemd/system/auditd.service
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/blk-availability.service
/usr/lib/systemd/system/brandbot.service
...

特點:
    不需要可執行權限,其內容也不能執行
    僅是systemd的調用的配置文件
unit類型 作用
Service unit 管理系統服務,我們暫且只關心這個
Target unit 多個Unit構成的一個組
Device Unit 硬件設備
Mount Unit 文件系統的掛載點
Automount Unit 自動掛載點
Path Unit 文件或路徑
Scope Unit 不是由 Systemd 啟動的外部進程
Slice Unit 進程組
Snapshot Unit Systemd 快照,可以切回某個快照
Socket Unit 進程間通信的 socket
Swap Unit swap 文件
Timer Unit 定時器

centos6 chkconfig VS 使用systemd的service unit治理centos7服務

任務 舊指令 新指令
哪些服務正在跑? chkconfig --list\grep 3:on systemctl list-units --type=service
哪些服務開機會自啟動? chkconfig --list systemctl list-unit-files --type=service
使httpd開機自啟動 chkconfig --level 3 httpd on systemctl enable httpd.service
使httd開機不自啟動 chkconfig --level 3 httpd off systemctl disable httpd.service
查看httpd現在是否啟動著 service httpd status systemctl status httpd.service (服務詳細信息)
啟動某服務 /etc/init.d/httpd start systemctl start httpd.service
停止某服務 /etc/init.d/httpd stop systemctl stop httpd.service
重啟某服務 /etc/init.d/httpd restart systemctl restart httpd.service
所有已裝載的級別(而非當前級別) runlevel systemctl list-units --type target
查看默認級別是啥 cat /etc/inittab systemctl get-default
設置默認級別 init 3 systemctl set-default multi-user.target
查看服務狀態 /etc/init.d/http status systemctl is-enabled httpd.service/systemctl status httpd.service
默認啟動第一個進程 init systemd
ls /usr/lib/systemd/system/*.service|more 下以service開頭的文件,都是和系統服務有關的, 其下還有別的後綴的服務.是別的unit的配置文件.

技術分享圖片

serivice unit配置

技術分享圖片

target unit: 的含義是服務組,表示一組服務。

將很多服務放在一坨, 開啟啟動時候加載這個文件.

啟動級別

/usr/lib/systemd/system
/etc/systemd/system

runlevel0.target -> poweroff.target
runlevel2.target -> multi-user.target
runlevel3.target -> multi-user.target
runlevel4.target -> multi-user.target
runlevel5.target -> graphical.target
runlevel6.target -> reboot.target

註意: centos7裏,運行級別234沒區別.

查看在運行的服務

我們看ACTIVE就ok了,SUB這一列可以不用理會

[root@n1 system]# systemctl list-units --type=service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
crond.service                      loaded active running Command Scheduler
dbus.service                       loaded active running D-Bus System Message Bus
docker.service                     loaded active running Docker Application Container Engine
[email protected]                 loaded active running Getty on tty1
httpd.service                      loaded active running The Apache HTTP Server

查看開機是否會啟動

[root@n1 system]# systemctl list-unit-files --type=service
UNIT FILE                                     STATE   
arp-ethers.service                            disabled
auditd.service                                disabled
[email protected]                               enabled #開機會啟動
blk-availability.service                      disabled#開機不會啟動
brandbot.service                              static  #這類不用管
conntrackd.service                            disabled

systemd管理服務入門

設置開機自動啟動細節

[root@n1 ~]# yum install psmisc -y
[root@n1 ~]# pstree
systemd─┬─agetty
        ├─crond
        ├─dbus-daemon
        ├─dockerd─┬─docker-containe─┬─docker-containe─┬─mysqld───20*[{mysqld}]
        │         │                 │                 └─8*[{docker-containe}]
        │         │                 └─9*[{docker-containe}]
        │         ├─docker-proxy───3*[{docker-proxy}]
        │         └─11*[{dockerd}]
        ├─httpd───8*[httpd]
        ├─lvmetad
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        └─systemd-udevd

[root@n1 system]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

相當於執行

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

disable相當於刪除

[root@n1 system]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@n1 system]# ll /etc/systemd/system/multi-user.target.wants
total 0
lrwxrwxrwx. 1 root root 37 Dec 27 04:44 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx  1 root root 38 Dec 26 21:05 docker.service -> /usr/lib/systemd/system/docker.service
lrwxrwxrwx  1 root root 37 Mar  4 03:39 httpd.service -> /usr/lib/systemd/system/httpd.service
lrwxrwxrwx. 1 root root 39 Dec 26 21:00 ntpdate.service -> /usr/lib/systemd/system/ntpdate.service
lrwxrwxrwx. 1 root root 40 Dec 27 04:44 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx. 1 root root 39 Dec 27 04:44 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx. 1 root root 36 Dec 27 04:44 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx. 1 root root 39 Dec 27 05:00 sysstat.service -> /usr/lib/systemd/system/sysstat.service

設置啟動級別的命令

- 設置默認啟動級別
[root@n1 ~]# systemctl set-default graphical.target 
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

- 查看默認的啟動級別
[root@n1 ~]# systemctl get-default 
graphical.target

- 手動修改啟動級別(先將原來的rm了)
[root@n1 ~]# ln -sv /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target
ln: failed to create symbolic link ‘/etc/systemd/system/default.target’: File exists
[root@n1 ~]# rm -rf /etc/systemd/system/default.target
[root@n1 ~]# ln -sv /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target
‘/etc/systemd/system/default.target’ -> ‘/usr/lib/systemd/system/multi-user.target’
[root@n1 ~]# systemctl get-default 
multi-user.target

- 查看服務狀態
[root@n1 ~]# systemctl is-enabled httpd.service 
disabled

[svc]centos7的服務治理-systemd