centos7上systemd詳解

centos7上systemd詳解

96 六絃極品 關注html

2018.07.21 16:39* 字數 1063 閱讀 291評論 0喜歡 0nginx

CentOS 7繼承了RHEL 7的新的特性,如強大的systemd, 而systemd的使用也使得以往系統服務的/etc/init.d的啓動腳本的方式就此改變, 也大幅提升了系統服務的運行效率。但服務的配置和以往也發生了極大的不一樣,同時變的簡單而易用了許多。web

CentOS 7的服務systemctl腳本存放在:/usr/lib/systemd/,有系統 system 和用戶 user 之分, 即:/usr/lib/systemd/system 和 /usr/lib/systemd/usercentos

1、配置文件

這裏先說明一下unit的文件位置,通常主要有三個目錄:socket

/lib/systemd/system
/run/systemd/system
/etc/systemd/system

這三個目錄的配置文件優先級依次從低到高,若是同一選項三個地方都配置了,優先級高的會覆蓋優先級低的。 系統安裝時,默認會將unit文件放在/lib/systemd/system目錄。若是想要修改系統默認的配置,好比nginx.service,通常有兩種方法:ide

一、在/etc/systemd/system目錄下建立nginx.service文件,裏面寫上咱們本身的配置。
在/etc/systemd/system下面建立nginx.service.d目錄,在這個目錄裏面新建任何以.conf結尾的文件,而後寫入本身的配置。推薦這種作法。測試

二、/run/systemd/system這個目錄通常是進程在運行時動態建立unit文件的目錄,通常不多修改,除非是修改程序運行時的一些參數時,即Session級別的,纔在這裏作修改。ui

2、服務配置

每個服務以.service結尾,通常會分爲3部分:[Unit]、[Service]和[Install],nginx爲例,具體內容以下:centos7

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3、配置項說明

下面分別解釋下着三部分的含義spa

  • [Unit]
    Description : 服務的簡單描述
    Documentation : 服務文檔
    After : 依賴,僅當依賴的服務啓動以後再啓動自定義的服務單元

  • [Service]
    Type : 啓動類型 simple、forking、oneshot、notify、dbus
    Type=simple(默認值):systemd認爲該服務將當即啓動,服務進程不會fork。若是該服務要啓動其餘服務,不要使用此類型啓動,除非該服務是socket激活型
    Type=forking:systemd認爲當該服務進程fork,且父進程退出後服務啓動成功。對於常規的守護進程(daemon),除非你肯定此啓動方式沒法知足需求, 使用此類型啓動便可。使用此啓動類型應同時指定PIDFile=,以便systemd可以跟蹤服務的主進程。
    Type=oneshot:這一選項適用於只執行一項任務、隨後當即退出的服務。可能須要同時設置 RemainAfterExit=yes 使得 systemd 在服務進程退出以後仍然認爲服務處於激活狀態。
    Type=notify:與 Type=simple 相同,但約定服務會在就緒後向 systemd 發送一個信號,這一通知的實現由 libsystemd-daemon.so 提供
    Type=dbus:若以此方式啓動,當指定的 BusName 出如今DBus系統總線上時,systemd認爲服務就緒。

PIDFile : pid文件路徑
ExecStartPre :啓動前要作什麼,上文中是測試配置文件 -t
ExecStart:啓動
ExecReload:重載
ExecStop:中止
PrivateTmp:True表示給服務分配獨立的臨時空間

  • [Install]
    WantedBy:服務安裝的用戶模式,從字面上看,就是想要使用這個服務的有是誰?上文中使用的是:multi-user.target ,就是指想要使用這個服務的目錄是多用戶。
    每個.target其實是連接到咱們單位文件的集合,當咱們執行:
systemctl enable nginx.service

就會在 /etc/systemd/system/multi-user.target.wants/ 目錄下新建一個 /usr/lib/systemd/system/nginx.service 文件的連接。

4、操做示例

下面是幾個最經常使用的service操做:

自啓動
systemctl enable nginx.service

禁止自啓動
systemctl disable nginx.service

啓動服務
systemctl start nginx.service

中止服務
systemctl stop nginx.service

重啓服務
systemctl restart nginx.service

查看Unit定義文件
systemctl cat nginx.service

編輯Unit定義文件
systemctl edit nginx.service

從新加載Unit定義文件
systemctl reload nginx.service

列出已啓動的全部unit,就是已經被加載到內存中
systemctl list-units

列出系統已經安裝的全部unit,包括那些沒有被加載到內存中的unit
systemctl list-unit-files

查看服務的日誌
journalctl -u nginx.service    # 還能夠配合`-b`一塊兒使用,只查看自本次系統啓動以來的日誌

查看全部target下的unit
systemctl list-unit-files --type=target

查看默認target,即默認的運行級別。對應於舊的`runlevel`命令
systemctl get-default

設置默認的target
systemctl set-default multi-user.target

查看某一target下的unit
systemctl list-dependencies multi-user.target

切換target,不屬於新target的unit都會被中止
systemctl isolate multi-user.target

關機
systemctl poweroff   
 
重啓
systemctl reboot       

進入rescue模式
systemctl rescue

參考資料

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html
https://www.nginx.com/resources/wiki/start/topics/examples/systemd/
http://time-track.cn/systemd-introduction.html

小禮物走一走,來簡書關注我

相關文章
相關標籤/搜索