控制服務和守護進程
1.systemd
1.1.systemd簡介
systemd是用戶空間的第一個應用程序,即 /sbin/init
init程序的類型:
SysV風格:init(centos5),實現系統初始化時,隨后的初始化操作都是借助於腳本來實現
特點:
腳本中含有大量的命令,每個命令都要啟動一個進程,命令執行完以后就要終止這個進程。如此一來,系統初始化時將大量的創建進程,銷毀進程,工作效率會非常低。
服務間可能會存在依賴關系,必須嚴格按照一定的順序來啟動服務,前一個服務沒啟動完后面的服務就無法執行啟動過程。不能並行進行。
配置文件:/etc/inittab
Upstart風格:init(centos6),由ubuntu研發的,通過總線形式接近於並行的方式工作,效率比Sysv高。
特點:
基於總線方式能夠讓進程間互相通信的一個應用程序
不用等服務啟動完成,只要一初始化就可以把自己的狀態返回給其他進程
配置文件:/etc/inittab,/etc/init/*.conf
Systemd風格:systemd(centos7)
特點:啟動速度比SysV和Upstar都快
不需要通過任何腳本來啟動服務,systemd自身就可以啟動服務,其本身就是一個強大的解釋器,啟動服務時不需要sh/bash的參與
systemd不真正在系統初始化時去啟動任何一個服務
只要服務沒有用到,它告訴你啟動了,實際上並沒有啟動。僅當第一次去訪問時才會真正啟動服務
配置文件:/usr/lib/systemd/system,/etc/systemd/system
系統啟動和服務器進程由systemd系統和服務管理器進行管理。此程序提供了一種方式,可以在啟動時和運行中的系統上激活系統資源、服務器守護進程和其他進程。
守護進程是在執行各種任務的后台等待或運行的進程。為了傾聽連接,守護進程使用套接字。套接字可以由守護進程創建,或者與守護進程分離,並且可能由另一個進程創建(如systemd),隨后在客戶端建立連接時將套接字傳遞到守護進程。
服務通常指的是一個或多個守護進程,但啟動或停止一項服務可能會對系統的狀態進行一次性更改(如配置網絡接口),不會留下守護進程之后繼續運行。
1.2.systemd的新特性
系統引導時實現服務並進行啟動
按需激活進程
系統狀態快照
基於依賴關系定義服務控制邏輯
1.3.systemd的核心概念Unit
systemd使用unit的概念來管理服務,這些unit表現為一個個配置文件。
systemd通過對這些配置文件進行標識和配置達到管理服務的目的:
//這些unit文件中主要包含了系統服務、監聽socket、保存的系統快照
//及其它與init相關的信息保存至以下目錄:
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system
Unit的類型:
Service unit //文件擴展名為.service,用於定義系統服務
Target unit //文件擴展名為.target,用於模擬實現“運行級別”
runlevel0.target和poweroff.target //關機
runlevel1.target和rescue.target //單用戶模式
runlevel2.target和multi-user.target //對於systemd來說,2/3/4級別沒有區別
runlevel3.target和multi-user.target //對於systemd來說,2/3/4級別沒有區別
runlevel4.target和multi-user.target //對於systemd來說,2/3/4級別沒有區別
runlevel5.target和graphical.target //圖形級別
runlevel6.target和reboot.target //重啟
Device unit //文件擴展名為.device,用於定義內核識別的設備
Mount unit //文件擴展名為.mount,用於定義文件系統掛載點
Socket unit //文件擴展名為.socket,用於標識進程間通信用的socket文件
Snapshot unit //文件擴展名為.snapshot,用於管理系統快照
Swap unit //文件擴展名為.swap,用於標識swap設備
Automount unit //文件擴展名為.automount,用於實現文件系統的自動掛載點
Path unit //文件擴展名為.path,用於定義文件系統中的一個文件或目錄
Unit關鍵特性
//基於socket的激活機制:
socket與服務程序分離,當有人去訪問時才會真正啟動服務,以此來實現按需激活進程與服務的並行啟動
//基於bus的激活機制:
所有使用dbus實現進程間通信的服務,可以在第一次被訪問時按需激活
//基於device的激活機制:
支持基於device激活的系統服務,可以在特定類型的硬件接入到系統中時,按需激活其所需要用到的服務
//基於path的激活機制:
某個文件路徑變得可用,或里面出現新文件時就激活某服務
//系統快照:
保存各unit的當前狀態信息於持久存儲設備中,必要時能自動載入
//向后兼容sysv init腳本
不兼容特性
//systemctl命令固定不變
//非由systemd啟動的服務,systemctl無法與之通信
//只有已經啟動的服務在級別切換時才會執行stop,在centos6以前是所有S開頭的服務全部start,所有K開頭的服務全部stop
//系統服務不會讀取任何來自標准輸入的數據流
//每個服務的unit操作均受5分鍾超時時間限制
2.使用systemctl管理服務
//語法:systemctl COMMAND name[.service|.target]
//常用COMMAND:
start name.service //啟動服務
stop name.service //停止服務
restart name.service //重啟服務
status name.service //查看服務狀態
[root@localhost ~]# systemctl stop postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 三 2019-09-25 15:11:46 CST; 23s ago
Process: 2146 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
Process: 1177 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
Process: 1165 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
Process: 1097 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
Main PID: 1321 (code=killed, signal=TERM)
9月 25 10:06:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 10:06:43 localhost.localdomain postfix/postfix-script[1319]: starting the Postfix mail system
9月 25 10:06:43 localhost.localdomain postfix/master[1321]: daemon started -- version 2.10.1, configuration ...fix
9月 25 10:06:43 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:11:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:11:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl start postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2019-09-25 15:12:51 CST; 9s ago
Process: 2146 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
Process: 2168 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
Process: 2165 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
Process: 2163 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
Main PID: 2240 (master)
CGroup: /system.slice/postfix.service
├─2240 /usr/libexec/postfix/master -w
├─2241 pickup -l -t unix -u
└─2242 qmgr -l -t unix -u
9月 25 15:12:51 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:12:51 localhost.localdomain postfix/master[2240]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:12:51 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl restart postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2019-09-25 15:14:20 CST; 16s ago
Process: 2250 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
Process: 2264 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
Process: 2262 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
Process: 2259 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
Main PID: 2336 (master)
CGroup: /system.slice/postfix.service
├─2336 /usr/libexec/postfix/master -w
├─2337 pickup -l -t unix -u
└─2338 qmgr -l -t unix -u
9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
try-restart name.service //條件式重啟服務,若服務已經啟動則重啟,若服務未啟動則不做任何操作
[root@localhost ~]# systemctl stop postfix.service
[root@localhost ~]# systemctl try-restart postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 42s ago
Process: 2346 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
Process: 2264 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
Process: 2262 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
Process: 2259 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
Main PID: 2336 (code=killed, signal=TERM)
9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
reload-or-restart name.service //重載或重啟服務,能reload則reload,否則restart
reload-or-try-restart name.service //重載或條件式重啟服務,能reload則reload,否則try-restart
mask name.service //禁止設定為開機自啟
unmask name.service //取消禁止設定為開機自啟
[root@localhost ~]# systemctl mask postfix.service
Created symlink from /etc/systemd/system/postfix.service to /dev/null.
[root@localhost ~]# systemctl status postfix.service
● postfix.service
Loaded: masked (/dev/null; bad)
Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 3min 11s ago
Main PID: 2336 (code=killed, signal=TERM)
9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl unmask postfix.service
Removed symlink /etc/systemd/system/postfix.service.
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 3min 27s ago
Main PID: 2336 (code=killed, signal=TERM)
9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
list-dependencies name.service //查看服務的依賴關系
is-active name.service //查看某服務當前激活與否的狀態
is-enable name.service //查看服務是否開機自動啟動
[root@localhost ~]# systemctl list-dependencies postfix.service
postfix.service
● ├─system.slice
● └─basic.target
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
● ├─rhel-dmesg.service
● ├─rhel-loadmodules.service
● ├─selinux-policy-migrate-local-changes@targeted.service
● ├─paths.target
● ├─slices.target
● │ ├─-.slice
● │ └─system.slice
● ├─sockets.target
● │ ├─dbus.socket
● │ ├─dm-event.socket
● │ ├─systemd-initctl.socket
● │ ├─systemd-journald.socket
● │ ├─systemd-shutdownd.socket
● │ ├─systemd-udevd-control.socket
● │ └─systemd-udevd-kernel.socket
● ├─sysinit.target
● │ ├─dev-hugepages.mount
● │ ├─dev-mqueue.mount
● │ ├─kmod-static-nodes.service
● │ ├─lvm2-lvmetad.socket
● │ ├─lvm2-lvmpolld.socket
● │ ├─lvm2-monitor.service
● │ ├─plymouth-read-write.service
lines 2-30
[root@localhost ~]# systemctl is-active postfix.service
active
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2019-09-25 15:22:42 CST; 4min 22s ago
Process: 2413 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
Process: 2411 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
Process: 2408 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
Main PID: 2485 (master)
CGroup: /system.slice/postfix.service
├─2485 /usr/libexec/postfix/master -w
├─2486 pickup -l -t unix -u
└─2487 qmgr -l -t unix -u
9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
enable name.service //設定某服務開機自動啟動
disable name.service //禁止服務開機自動啟動
[root@localhost ~]# systemctl enable postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2019-09-25 15:22:42 CST; 8min ago
Main PID: 2485 (master)
CGroup: /system.slice/postfix.service
├─2485 /usr/libexec/postfix/master -w
├─2486 pickup -l -t unix -u
└─2487 qmgr -l -t unix -u
9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl disable postfix.service
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2019-09-25 15:22:42 CST; 10min ago
Main PID: 2485 (master)
CGroup: /system.slice/postfix.service
├─2485 /usr/libexec/postfix/master -w
├─2486 pickup -l -t unix -u
└─2487 qmgr -l -t unix -u
9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
isolate name.target //切換至某級別,如systemctl isolate graphical.target就是切換至圖形界面
list-unit-files --type service //查看所有服務的開機自動啟動狀態(是否開機自啟)
list-units --type service //查看所有已經激活的服務狀態信息
list-units --type target //查看所有已裝載的級別
list-units --type service --all //查看所有服務(已啟動/已停止)的狀態信息
list -units --type target --all //查看所有的級別
[root@localhost ~]# systemctl list-unit-files --type service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
autovt@.service enabled
blk-availability.service disabled
brandbot.service static
chrony-dnssrv@.service static
chrony-wait.service disabled
chronyd.service enabled
console-getty.service disabled
console-shell.service disabled
container-getty@.service static
cpupower.service disabled
crond.service enabled
dbus-org.fedoraproject.FirewallD1.service enabled
dbus-org.freedesktop.hostname1.service static
dbus-org.freedesktop.import1.service static
dbus-org.freedesktop.locale1.service static
dbus-org.freedesktop.login1.service static
dbus-org.freedesktop.machine1.service static
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service enabled
dbus-org.freedesktop.timedate1.service static
dbus.service static
debug-shell.service disabled
dm-event.service disabled
dracut-cmdline.service static
dracut-initqueue.service static
dracut-mount.service static
get-default //查看默認運行級別
set-default name.target //設置默認運行級別
[root@localhost ~]# systemctl get-default
multi-user.target
[root@localhost ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
rescue //切換至緊急救援模式(大多數服務不啟動,但是會加載驅動)
emergency //切換至emergency模式(驅動不會加載,系統不會初始化,服務不會啟動)
halt //關機
poweroff //關機
reboot //重啟
suspend //掛起系統,此時不能關機,否則無用
hibernate //創建並保存系統快照,下次系統重啟時會自動載入快照
hybrid-sleep //混合睡眠,快照並掛起