1民珍、簡述systemd的新特性
????????RedHat 系的Linux系統(tǒng)队魏,init進(jìn)程的有:
????????CentOS 5 : SysV-->init
????????CentOS 6 : Upstart-->init
????????CentOS 7: Systemd
其中巩螃,Systemd 的新特性有:
????????系統(tǒng)引導(dǎo)時實(shí)現(xiàn)進(jìn)程并行啟動届惋;
????????可以按需啟動進(jìn)程;也可以根據(jù)依賴性進(jìn)行服務(wù)控制掠手;
????????系統(tǒng)狀態(tài)快照;
2柳恐、Unit?(單元)
Systemd 是通過unit 及其配置文件來管理控制系統(tǒng)服務(wù)伐脖、監(jiān)聽的socket热幔、快照及其他與init相關(guān)的信息的。
unit 配置文件通常放置于:
? ? ? ? ? /usr/lib/systemd/system 讼庇、/etc/systemd/system绎巨、/run/systemd/system
unit的常見類型:
? ? ? ? Service unit:? 配置文件以.service 結(jié)尾,用于定義系統(tǒng)服務(wù)
? ? ? ? Target unit: 以.target 結(jié)尾, 用于模擬運(yùn)行級別
? ? ? ? Device unit : 以.device 結(jié)尾蠕啄,用于定義內(nèi)核識別的設(shè)備场勤;
? ? ? ? Socket unit:? 以.socket 結(jié)尾, 用于定義進(jìn)程間通訊的socket文件
? ? ? ? Path unit : 以.path 結(jié)尾,用于定義文件系統(tǒng)中的文件或目錄;
? ? ? ? Mount unit : 以.mount 結(jié)尾,用于定義系統(tǒng)掛載點(diǎn)
? ? ? ? Automount unit: 以.automount結(jié)尾, 用于定義系統(tǒng)的自動掛載點(diǎn)
? ? ? ? Swap unit :? 以.swap 結(jié)尾, 用于標(biāo)識swap設(shè)備;
? ? ? ? Snapshot unit : 以.snapshot 結(jié)尾歼跟, 用于管理系統(tǒng)快照;
比如和媳, service unit 配置文件 內(nèi)容如下:??
>>>> cat /usr/lib/systemd/system/firewalld.service
[Unit]
Description=firewalld - dynamic firewall daemon
Before=network-pre.target
Wants=network-pre.target
After=dbus.service
After=polkit.service
Conflicts=iptables.service ip6tables.service ebtables.service ipset.service
Documentation=man:firewalld(1)
[Service]
EnvironmentFile=-/etc/sysconfig/firewalld
ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS
ExecReload=/bin/kill -HUP $MAINPID
# supress to log debug and error output also to /var/log/messages
StandardOutput=null
StandardError=null
Type=dbus
BusName=org.fedoraproject.FirewallD1
KillMode=mixed
[Install]
WantedBy=multi-user.target
Alias=dbus-org.fedoraproject.FirewallD1.service
從上可以看出, unit 配置文件一般分為三個部分:
? ? ? ? unit : 定義unit 的依賴性、幫助哈街、描述等信息
? ??????????????????Description : unit 意義描述
????????????????????Before: 定義當(dāng)前unit應(yīng)該先于哪些unit 啟動留瞳;
????????????????????After: 定義當(dāng)前unit 應(yīng)該晚于哪些unit 啟動;
? ??????????????????Wants:定義當(dāng)前unit 依賴哪些units ; 弱依賴,即其他units 可有可無;
? ? ? ? ? ? ? ? ? ?Conflicts: 定義沖突的units?
????????????????????Documentation: 幫助信息
? ? ? ? service:定義unit 的相關(guān)選項(xiàng)
? ??????????????????EnvironmentFile: 定義unit 環(huán)境變量信息
????????????????????ExecStart:指明啟動unit 時運(yùn)行的腳本
? ??????????????????ExecStop:指明停止unit 時運(yùn)行的腳本
????????????????????ExecReload:指明重載unit時運(yùn)行的腳本
????????????????????Type:Configures the unit process startup type that affects the functionality of?ExecStart?and related options. /? 配置影響ExecStart及相關(guān)選項(xiàng)的 unit進(jìn)程啟動類型
????????????????????????????simple?– The process started with?ExecStart?is the main process of the service./默認(rèn)選項(xiàng)骚秦,ExecStart 啟動的進(jìn)程是主進(jìn)程;
????????????????????????????forking?– The process started with?ExecStart?spawns a child process that becomes the main process of the service. The parent process exits when the startup is complete./ExecStart 啟動的進(jìn)程會啟動一個子進(jìn)程她倘,子進(jìn)程將會成為服務(wù)的主進(jìn)程。父進(jìn)程在服務(wù)啟動完成之后退出.
????????????????????????????oneshot?– This type is similar to?simple, but the process exits before starting consequent units./ 類似于simple,但是這個進(jìn)程在啟動后續(xù)的unit之前就退出了.
????????????????????????????dbus?– This type is similar to?simple, but consequent units are started only after the main process gains a D-Bus name. /類似于simple,但是只有該主進(jìn)程收到一個D-Bus名稱后才會啟動后續(xù)units .
????????????????????????????notify?– This type is similar to?simple, but consequent units are started only after a notification message is sent via the sd_notify() function. / 類似simple作箍,后續(xù)的units 只有在主進(jìn)程收到sd_notify()函數(shù)的通知后才會啟動;
? ? ? ? ? ? ? ? ? ? ? ? ? ? idle?– similar to?simple, the actual execution of the service binary is delayed until all jobs are finished, which avoids mixing the status output with shell output of services./ 服務(wù)的二進(jìn)制程序被延遲執(zhí)行硬梁,直到所有的任務(wù)完成,以避免狀態(tài)輸出與服務(wù)器的shell 輸出混合;
?????????install?:This section is not interpreted by systemd(1) during runtime. It is used exclusively by the enable and disable commands of the systemctl(1) tool during installation of a unit./這部分在運(yùn)行期間是不會被執(zhí)行胞得。它只在執(zhí)行systemctl enable|disable時使用;
3荧止、實(shí)現(xiàn)systemd 管理niginx 服務(wù)
? ? ? ? 3.1 將nginx 源碼包安裝至/usr/local/nginx 下;
? ? ? ?3.2 創(chuàng)建unit配置文件
?????????????>>> cat /usr/lib/systemd/system/nginx.service
????????????[Unit]
????????????Description=nginx
????????????[Service]
? ? ? ? ? ?ExecStart=/usr/local/nginx/sbin/nginx
????????????ExecStop=/usr/local/nginx/sbin/nginx -s stop
????????????ExecReload=/usr/local/nginx/sbin/nginx -s reload
????????????Type=forking
? ??????