Skip to content

Instantly share code, notes, and snippets.

@istar0me
Created November 6, 2018 02:09
Show Gist options
  • Save istar0me/2e8476b6b7b11452687bfbe3b4e11d2a to your computer and use it in GitHub Desktop.
Save istar0me/2e8476b6b7b11452687bfbe3b4e11d2a to your computer and use it in GitHub Desktop.

systemd 線上課程

  • 介绍systemd功能-51CTO学院
  • rpm:軟體管理的工具
  • rpm -ql systemd:查詢套件所安裝的檔案清單
    • q:query
    • l:list
  • systemctl等同於systemctl list-units
  • systemctl --state=all:所有啟動的狀態
    • systemctl --state=active:啟動成功
    • systemctl --state=inactive:未啟動
    • systemctl --state=failed:啟動失敗
  • systemctl list-unit-files
  • - vs. --
    • -:指令的參數
    • --:參數的參數(還有其它的意思)
  • kill 只能一次殺掉一個 process,但是如果一次要刪掉大量的 process(如 apache 伺服器的 httpd 服務一次會開啟多個 procees),可輸入 pkill httpd 來一次殺掉大量 process

  • systemctl enable httpd:開機時自動啟動 httpd 服務
    • 這個指令會建立一個符號連結(symlink)
    • 可透過 systemctl status httpdsystemctl is-enabled httpd 確認開機是否自啟
  • systemctl is-active httpd 能查看服務是否啟動中
    • systemctl start httpd
    • systemctl stop httpd
    • systemctl restart httpd

開、關與重開機

  • systemctl reboot
  • systemctl poweroff
  • systemctl shutdown
  • systemctl start

查看服務檔案

  • systemctl cat sshd.service:查看指定服務檔案的內容
    • 或是輸入完整路徑,如 cat /usr/lib/systemd/system/sshd.service
    • .service 能省略,因此能寫成 systemctl cat sshd

檔案中有 [Unit], [Service], [Install] 三個區塊

  • [Unit]
    • EnvironmentFile:設定檔案位置
    • Wants:所需要的服務
  • [Service]
    • ExecReload:如果有這個選項,代表能輸入 systemctl reload crond 來重新啟動
      • 如果能用 reload 先用它,不行再用 restart
  • [Install]
    • WantedBy:創建符號連結的位置
    • 如 WantedBy 為 multi-user.target 會創建符號連結到 /etc/systemd/system/multi-user.target.wants

分析 Unit 依賴關係

  • [Unit] 中有 AfterWants
    • Wants:弱依賴
    • Requires:強依賴,所依賴的服務必須執行中
    • Before
    • After
  • TODO:如何在 man 中搜尋?
  • systemctl list-dependencies sshd:列出所依賴的服務

整體替換 service 文件

  • /etc/systemd/system/ 權重較 /usr/lib/systemd/system/ 目錄高,因此如果兩個資料夾有同名稱的檔案,會先執行前者目錄內的檔案
  • systemctl edit --full sshd:編輯 sshd 服務的檔案
    • --full:TODO:代表的意思?

局部替換 service 文件

期中考

  • 考試範圍:C10 ~ C14
  • 後面習題記得要寫
@blackstuend
Copy link

systemctl cat 我在我的centos 沒有這個command?

@blackstuend
Copy link

學到了很多感謝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment