Centos7 systemd

Centos7 systemd 基礎學習

系統(tǒng)開機啟動流程:

POST--> boot sequence --> bootloader --> kernel + initramfs(initrd)--> rootfs  --> /sbin/init
# 又想起了加載kernel時,kernel是在文件系統(tǒng)中,所以還是需要先掛載文件系統(tǒng),所以還是需要驅動,掛載文件系統(tǒng)的驅動在哪里呢?

init:

centos 5: sysv init
centos 6: upstart
centos 7: systemd   向后不完全兼容sysv init,upstart

systemd新特性:

> 系統(tǒng)引導時,實現服務并行啟動
> 按需激活進程
> 系統(tǒng)狀態(tài)快照
> 基于依賴關系定義服務控制邏輯

核心概念: unit(單元)

> 配置文件進行標識和配置,文件中主要包含了系統(tǒng)服務,監(jiān)聽socket,保存系統(tǒng)快照以及其他與init相關信息。
> 保存位置:(常用位置)
    > /usr/lib/systemd/system
    > /run/systemd/system
    > /etc/systemd/system

Unit類型:

> Service unit: 文件擴展名為.service 用于定義系統(tǒng)服務
> Target unit: 文件擴展名為.target,用于模擬實現“運行級別”,但并不完全是。
> Device unit: .device 用于定義內核識別的設備
> Mount unit: .mount,定義文件系統(tǒng)掛載點
> socket unit: .socket,用于標識進程間通信用的socket文件
> snapshot unit: .snapshot,管理系統(tǒng)快照
> Swap unit: .swap,用于標識swap設備
> Automount unit: .automount,文件系統(tǒng)的自動掛載點
> Path unit: .path,用于定義文件系統(tǒng)中的一個文件或目錄

關鍵特性:

基于socket的激活機制: socket與服務程序分離,當有進程需要訪問此socket才真正啟動
基于bus的激活機制
基于device的激活機制:類似插入u盤,需要激活mount服務來掛載設備
基于path的激活機制:監(jiān)控文件路徑或目錄發(fā)生變化,立即激活某服務
系統(tǒng)快照:將unit當前狀態(tài)數據保存在持久設備中
向后兼容sysv init腳本

不兼容:

systemctl命令固定不變,不像sysv init腳本可以自由定義。
非由systemd啟動的服務,systemctl無法與之通信。
5分鐘超時時間。

在Centos 5,6當中運行,級別切換過程(運行級別2切換到運行級別3):將級別3的k服務全部kill掉,s服務全部start

管理系統(tǒng)服務命令幫助:

Centos 7 : service unit,能兼容早期的服務腳本
命令: systemctl COMMAND  NAME.service
Unit Commands:
  list-units [PATTERN...]         List loaded units    (列出系統(tǒng)已經load的unit,-a查看所有的unit ,-t service查看service類型)
  list-sockets [PATTERN...]       List loaded sockets ordered by address   (列出系統(tǒng)已經加載socket類型unit)
  list-timers [PATTERN...]        List loaded timers ordered by next elapse
  start NAME...                   Start (activate) one or more units   (啟動服務)
  stop NAME...                    Stop (deactivate) one or more units  (停止服務)
  reload NAME...                  Reload one or more units             (重載服務)
  restart NAME...                 Start or restart one or more units   (重啟服務)
  try-restart NAME...             Restart one or more units if active
  reload-or-restart NAME...       Reload one or more units if possible,
                                  otherwise start or restart
  reload-or-try-restart NAME...   Reload one or more units if possible,
                                  otherwise restart if active
  isolate NAME                    Start one unit and stop all others   (切換運行級別)
  kill NAME...                    Send signal to processes of a unit   (向某unit進程發(fā)送信號)
  is-active PATTERN...            Check whether units are active
  is-failed PATTERN...            Check whether units are failed
  status [PATTERN...|PID...]      Show runtime status of one or more units
  show [PATTERN...|JOB...]        Show properties of one or more
                                  units/jobs or the manager
  cat PATTERN...                  Show files and drop-ins of one or more units
  set-property NAME ASSIGNMENT... Sets one or more properties of a unit
  help PATTERN...|PID...          Show manual for one or more units
  reset-failed [PATTERN...]       Reset failed state for all, one, or more
                                  units
  list-dependencies [NAME]        Recursively show units which are required
                                  or wanted by this unit or by which this
                                  unit is required or wanted

Unit File Commands:
  list-unit-files [PATTERN...]    List installed unit files  (列出unit服務開機啟動狀態(tài),-t service 僅查看service類型的服務)
  enable NAME...                  Enable one or more unit files  (開機啟動)
  disable NAME...                 Disable one or more unit files  (禁止開機啟動)
  reenable NAME...                Reenable one or more unit files
  preset NAME...                  Enable/disable one or more unit files
                                  based on preset configuration
  preset-all                      Enable/disable all unit files based on
                                  preset configuration
  is-enabled NAME...              Check whether unit files are enabled  (查看服務是否開機自啟動)
  mask NAME...                    Mask one or more units
  unmask NAME...                  Unmask one or more units
  link PATH...                    Link one or more units files into
                                  the search path
  add-wants TARGET NAME...        Add 'Wants' dependency for the target
                                  on specified one or more units
  add-requires TARGET NAME...     Add 'Requires' dependency for the target
                                  on specified one or more units
  edit NAME...                    Edit one or more unit files
  get-default                     Get the name of the default target   (查看默認允許級別)
  set-default NAME                Set the default target    (設置默認運行級別)

Machine Commands:
  list-machines [PATTERN...]      List local containers and host

Job Commands:
  list-jobs [PATTERN...]          List jobs
  cancel [JOB...]                 Cancel all, one, or more jobs

Snapshot Commands:
  snapshot [NAME]                 Create a snapshot    (創(chuàng)建unit快照)
  delete NAME...                  Remove one or more snapshots    (刪除unit快照)

Environment Commands:
  show-environment                Dump environment            (查看運行環(huán)境,如語言,搜索路徑)
  set-environment NAME=VALUE...   Set one or more environment variables
  unset-environment NAME...       Unset one or more environment variables
  import-environment [NAME...]    Import all or some environment variables

Manager Lifecycle Commands:
  daemon-reload                   Reload systemd manager configuration
  daemon-reexec                   Reexecute systemd manager

System Commands:
  is-system-running               Check whether system is fully running
  default                         Enter system default mode            
  rescue                          Enter system rescue mode             (緊急救援模式)
  emergency                       Enter system emergency mode          (emergency模式,比rescue模式更徹底)
  halt                            Shut down and halt the system        (關機)
  poweroff                        Shut down and power-off the system   (關機)
  reboot [ARG]                    Shut down and reboot the system      (重啟)
  kexec                           Shut down and reboot the system with kexec
  exit                            Request user instance exit
  switch-root ROOT [INIT]         Change to a different root file system   (切換根文件系統(tǒng))
  suspend                         Suspend the system     (掛起系統(tǒng))
  hibernate                       Hibernate the system  (保存系統(tǒng)快照)
  hybrid-sleep                    Hibernate and suspend the system    (保存系統(tǒng)快照并掛起)

運行級別:

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

運行級別切換:systemctl isolate graphical.target
不能使用runlevel切換運行級別,可以使用runlevel查看

切換至緊急求援模式:

systemctl rescue

切換至emergency模式:

systemctl emergency

設置服務開機自啟動:

systemctl enbale nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 只是在/etc/systemd/system/multi-user.target.wants/目錄下做了一個軟鏈接
# 禁止開機自啟動也只是將此目錄下的服務名稱軟鏈接刪除

類似的還有設置默認運行級別:

# 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.

思考:這樣的設計思想與git在分支管理,版本管理上是否有異曲同工之妙?

內容按需快照一份,設計一個head指針,將指針指向我們需要的內容位置即可,高效,靈活,易理解。
我們人的記憶不也類似這樣的一個工作方式,將記憶碎片重現。

unit編寫:

[Unit]
Description=The nginx HTTP and reverse proxy server        描述
After=network.target remote-fs.target nss-lookup.target    服務先后次序與依賴關系

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid    start前,先刪除是否有pid文件
ExecStartPre=/usr/sbin/nginx -t      start前,先測試配置文件準確性
ExecStart=/usr/sbin/nginx      啟動nginx  
ExecReload=/bin/kill -s HUP $MAINPID   重啟,即是向nginx進程發(fā)送HUP信號
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target     多用戶模式啟動

# unit 單元文件編寫:
# https://www.freedesktop.org/software/systemd/man/systemd.unit.html、
# http://blog.csdn.net/yuesichiu/article/details/51331136#t0
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容