天天看點

systemd簡介

systemd是Linux的一個工具,是CentOS7.x系統啟動的第一個程序

1.systemd的一些相關指令

(1)systemctl 系統管理

#檢查系統是否充分的運作

[root@localhost ~]# systemctl is-system-running

running

#進入系統預設模式

[root@localhost ~]# systemctl default

PolicyKit daemon disconnected from the bus.

We are no longer a registered authentication agent.

#進入營救模式

[root@localhost ~]# systemctl rescue

Broadcast message from [email protected] on pts/0 (Mon 2018-03-05 23:25:23 CST):

The system is going down to rescue mode NOW!

maintenance

#進入緊急情況模式

[root@localhost ~]# systemctl emergency

#進入冬眠模式

[root@localhost ~]# systemctl hibernate 

[root@localhost ~]# systemctl half

[root@localhost ~]# systemctl poweroff

[root@localhost ~]# systemctl reboot

(2)systemd-analyze檢視啟動耗時

[root@localhost ~]# systemd-analyze

Startup finished in 849ms (kernel) + 2.347s (initrd) + 20.205s (userspace) = 23.402s

#檢視每個服務啟動耗時

[root@localhost ~]# systemd-analyze blame

#檢視指定服務的啟動流

[root@localhost ~]# systemd-analyze critical-chain httpd.service

The time after the unit is active or started is printed after the "@" character.

The time the unit takes to start is printed after the "+" character.

httpd.service +115ms

└─system.slice

  └─-.slice

[root@localhost ~]# systemd-analyze critical-chain sshd.service

sshd.service @9min 39.109s

(3)hostnamectl

#檢視主機名等資訊

[root@localhost ~]# hostnamectl

#修改主機名,exit會話或者reboot生效

[root@localhost ~]# hostnamectl set-hostname

(4)localectl

#檢視本地化設定

[root@darren ~]# localectl

   System Locale:.UTF-8

       VC Keymap: us

      X11 Layout: us

[root@darren ~]# localectl set-locale.UTF-8

[root@darren ~]# localectl list-locales|grep 'zh_CN'

(5)timedatectl

[root@darren ~]# timedatectl

      Local time: Tue 2018-03-06 19:05:28 CST

  Universal time: Tue 2018-03-06 11:05:28 UTC

        RTC time: Tue 2018-03-06 12:22:53

       Time zone: Asia/Shanghai (CST, +0800)

     NTP enabled: n/a

NTP synchronized: no

RTC in local TZ: no

      DST active: n/a

[root@darren ~]# timedatectl list-timezones

#設定時間

[root@darren ~]# timedatectl set-time 2018-03-06

[root@darren ~]# timedatectl set-time 20:31

(6)loginctl使用者連接配接相關資訊

Tue Mar  6 20:31:53 CST 2018

[root@darren ~]# loginctl

   SESSION        UID USER             SEAT            

        19          0 root                             

        24          0 root                             

        23          0 root

[root@darren ~]# loginctl list-users

       UID USER            

         0 root

#檢視root使用者的詳細資訊

[root@darren ~]# loginctl show-user root

2.服務管理systemctl

(1)自啟動原理

對于支援systemd的軟體,安裝後,會自動在/usr/lib/systemd/system目錄下生成一個xxx.service配置檔案.

系統啟動的時候會讀取/etc/systemd/system下的配置檔案,是以會開啟自啟動。

例:

開啟httpd自啟動的實質是在/etc/systemd/system/multi-user.target.wants下建立一個軟連接配接指向/usr/lib/systemd/system/httpd.service檔案

[root@localhost system]# systemctl is-enabled httpd.service

disabled

[root@localhost system]# systemctl enable  httpd.service

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@localhost ~]# ll /etc/systemd/system/multi-user.target.wants/httpd.service

lrwxrwxrwx. 1 root root 37 Feb 24 04:03 /etc/systemd/system/multi-user.target.wants/httpd.service -> /usr/lib/systemd/system/httpd.service

啟動一個服務:systemctl start postfix.service

關閉一個服務:systemctl stop postfix.service

重新開機一個服務:systemctl restart postfix.service

顯示一個服務的狀态:systemctl status postfix.service

開啟開機自啟動:systemctl enable postfix.service

關閉開機自啟動:systemctl disable postfix.service

檢視服務是否開機啟動:systemctl is-enabled postfix.service

檢視服務是否活動:systemctl is-active httpd.service

檢視已啟動的服務清單:systemctl list-unit-files|grep enabled

删除httpd程序: systemctl kill httpd.service

修改配置檔案後,需要重新加載配置檔案: systemctl daemon-reload

檢視服務的依賴關系:systemctl list-dependencies httpd.service

(2)列出所有unit及配置檔案的狀态

配置檔案狀态種類:

enabled:已建立自啟動連結

disabled:沒有建立自啟動連結

static:該配置檔案沒有[Install]部分(無法執行),隻能作為其他配置檔案的依賴

masked:該配置檔案被禁止建立自啟動連結

[root@darren system]# systemctl list-unit-files

[email protected]                             disabled

brandbot.service                            static  

httpd.service                               enabled

(3)配置檔案的格式說明

[root@darren system]# cat httpd.service

[Unit]

Description=The Apache HTTP Server

After=network.target remote-fs.target nss-lookup.target

Documentation=man:httpd(8)

Documentation=man:apachectl(8)

[Service]

Type=notify

EnvironmentFile=/etc/sysconfig/httpd

#ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND

ExecReload=/usr/sbin/httpd $OPTIONS -k graceful

ExecStop=/bin/kill -WINCH ${MAINPID}

# We want systemd to give httpd some time to finish gracefully, but still want

# it to kill httpd after TimeoutStopSec if something went wrong during the

# graceful stop. Normally, Systemd sends SIGTERM signal right after the

# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give

# httpd time to finish.

KillSignal=SIGCONT

PrivateTmp=true

[Install]

WantedBy=multi-user.target

[Unit]區塊通常是配置檔案的第一個區塊,用來定義Unit的中繼資料,以及該服務與其他Unit的關系

Description:描述資訊

Documentation:文檔位址

After:在httpd之前需要啟動的unit

Before:在httpd之後需要啟動的unit

Requires:依賴的unit啟動,該unit才能啟動,如After子產品的unit沒有運作,目前Unit則不會啟動

Wants:和Requires相似,但是沒有它嚴格,如After子產品的unit沒有運作,目前Unit也可成功啟動個(推薦使用)

[Service]服務的配置

Type選項:

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認為服務就緒。

Type=idle :systemd會等待所有任務處理完成後,才開始執行 idle 類型的單元。其他行為與 Type=simple 類似。

[Install]通常是配置檔案的最後一個區塊,用來定義如何啟動,以及是否開機啟動。

3.日志管理journalctl

配置檔案:/etc/systemd/journald.conf

journalctl -b -0 顯示本次啟動的資訊

journalctl -b -1 顯示上次啟動的資訊

journalctl -b -2 顯示上上次啟動的資訊 journalctl -b -2

隻顯示錯誤、沖突和重要告警資訊

journalctl -p err..alert   等價于: journalctl -p 3..1

顯示從某個日期 ( 或時間 ) 開始的消息:

 journalctl --since="2012-10-30 18:17:16"

顯示從某個時間 ( 例如 20分鐘前 ) 的消息:

journalctl --since "20 min ago"

顯示最新資訊

 journalctl -f

顯示特定程式的所有消息:

# journalctl /usr/lib/systemd/systemd

顯示特定程序的所有消息:

journalctl _PID=1

參考連結:

https://wiki.archlinux.org/index.php/systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)#.E5.A4.84.E7.90.86.E4.BE.9D.E8.B5.96.E5.85.B3.E7.B3.BB

繼續閱讀