天天看點

systemd攻略之二:實戰unit,詳解static,target

一、導讀

2016年08月19日 Upstart 将被放棄,Ubuntu 投入 Systemd 懷抱

Canonical 的 Martin Pitt 宣布将不再使用他們自己的 Upstart 初始化系統來啟動 Ubuntu 桌面會話,取而代之的是更現代化的、卻仍有争議的 Systemd。

每次 Systemd 釋出,我們都對發現這個所謂的“初始化系統”又做了比原來的設計目标還要多得多的工作。它慢慢地接管了 GNU/Linux 作業系統越來越多的内部元件的工作,甚至,我們毫不懷疑,它将會完全取代它們,而這一天并不遠了,或許,将來你會看到 Systemd/Linux 作業系統——除了 Linux 核心,其它的都叫 Systemd。

Upstart 是 Canonical/Ubuntu 自己的項目,它同 Systemd 一樣,目标都是取代傳統的初始化系統,用在幾乎所有的 Ubuntu Linux 上。然而,從 Ubuntu 15.04 開始,Ubuntu 開始逐漸使用 Systemd 替代 Upstart 初始化系統,這讓許多使用者很憤怒。

争議歸争議,然而是好是壞,從我這幾天慢慢對systemd的接觸,發現其确實有其魅力之處。

二、手把手帶你寫unit

2.1 bb service依賴aa service

下面,我帶大家建立兩個service類型unit,aa和bb,其中bb的啟動依賴aa,即,如果你将bb啟動,aa自然跟着啟動,如果,此時将A關閉,B也會随之關閉。這個主要是要在B的unit檔案中配置一個參數:Requires=aa.service

建立unit檔案要點

  • service unit最重要的是service區塊,而且ExecStart參數很重要,這個參數寫上你這個service要執行的腳本的路徑
  • ExecStart=/usr/libexec/aa 這個表示執行這個unit,需要執行的腳本。切記,chmod +x /usr/libexec/aa 加可執行權限
  • type類型的選擇,根據自己的需求,man 5 systemd.unit看差別

建立一個aa service

#vim /etc/systemd/system/aa.service
[Unit]
Description=aa
After=syslog.target network.target nss-lookup.target
Before=time-sync.target
Wants=time-sync.target
[Service]
Type=simple
ExecStart=/usr/libexec/aa
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target      
#vim /usr/libexec/aa
#!/bin/bash
for i in `seq 1 100`;do
    DATE=`date`
    echo $DATE >> /var/log/aa
    sleep 0.01
done      

建立一個bb service

#vim /etc/systemd/system/bb.service
[Unit]
Description=bb
After=syslog.target network.target nss-lookup.target
Before=time-sync.target
Wants=time-sync.target
Requires=aa.service
[Service]
Type=simple
ExecStart=/usr/libexec/bb
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target      
#vim /usr/libexec/bb
#!/bin/bash
DATE=`date`
echo $DATE >> /var/log/bb      

驗證

目前aa bb都是stop的狀态

#systemctl status bb aa      

隻啟動bb

#systemctl start bb      

發現aa bb都啟動了,這是因為bb的啟動依賴aa

#systemctl status bb aa
● bb.service - bb
   Loaded: loaded (/etc/systemd/system/bb.service; disabled; vendor preset: disabled)
   Active: active (exited) since Thu 2016-08-18 23:51:04 CST; 5s ago
  Process: 39662 ExecStart=/usr/libexec/bb (code=exited, status=0/SUCCESS)
 Main PID: 39662 (code=exited, status=0/SUCCESS)
Aug 18 23:51:04 localhost systemd[1]: Started bb.
Aug 18 23:51:04 localhost systemd[1]: Starting bb...
● aa.service - aa
   Loaded: loaded (/etc/systemd/system/aa.service; enabled; vendor preset: disabled)
   Active: active (exited) since Thu 2016-08-18 23:51:05 CST; 4s ago
  Process: 39663 ExecStart=/usr/libexec/aa (code=exited, status=0/SUCCESS)
 Main PID: 39663 (code=exited, status=0/SUCCESS)
Aug 18 23:51:04 localhost systemd[1]: Starting aa...
Aug 18 23:51:05 localhost systemd[1]: Started aa.      

然後看看執行結果

#tail /var/log/aa
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016
Thu Aug 18 23:51:05 CST 2016      
#tail /var/log/bb
Thu Aug 18 17:35:03 CST 2016
Thu Aug 18 17:37:19 CST 2016
Thu Aug 18 17:40:06 CST 2016
Thu Aug 18 17:41:18 CST 2016
Thu Aug 18 23:49:02 CST 2016
Thu Aug 18 23:49:21 CST 2016
Thu Aug 18 23:51:04 CST 2016      

三、幾個牛逼的指令

檢視aa unit檔案

#systemctl cat aa      

編輯aa unit檔案

推薦使用這個
#systemctl edit --full aa      
不推薦使用這個,這個是分片的方式改unit檔案
#systemctl edit aa      

其實systemctl status很強大

其實#systemctl status aa 檢視一個unit的狀态,這個指令的輸出很強大,一開始,大家都不覺得,因為看不懂這個輸出。什麼exit,什麼active,什麼enable,實在是不知道這個unit到底是幾個意思?

栗子輸出

#systemctl status aa
● aa.service - aa
   Loaded: loaded (/etc/systemd/system/aa.service; enabled; vendor preset: disabled)
   Active: active (exited) since Thu 2016-08-18 23:51:05 CST; 7min ago
 Main PID: 39663 (code=exited, status=0/SUCCESS)
Aug 18 23:51:04 localhost systemd[1]: Starting aa...
Aug 18 23:51:05 localhost systemd[1]: Started aa.      
#systemctl status aa
● aa.service - aa
   Loaded: loaded (/etc/systemd/system/aa.service; enabled; vendor preset: disabled)
   #loaded表示unit檔案已經被systemd識别,并且加載到記憶體中;
   Active: active (exited) since Thu 2016-08-18 23:51:05 CST; 7min ago
 Main PID: 39663 (code=exited, status=0/SUCCESS)
    # exited表示,這個ExecStart後面的腳本已經執行結束,并且有傳回值,後面有SUCCESS表示,這個腳本結束,沒有問題。
    #code 的值,可以有很多種,根據unit的類型輸出;
Aug 18 23:51:04 localhost systemd[1]: Starting aa...
Aug 18 23:51:05 localhost systemd[1]: Started aa.      

我分不清systemctl list-units 和systemctl list-unit-files

也許最讓你迷惑的是

#systemctl list-units  顯示所有active狀态的unit檔案
#systemctl list-units --all  顯示所有包含inactive狀态的unit檔案
#systemctl list-unit-files   顯示所有unit檔案,不care systemd到底有沒有關聯到這些unit檔案      
  • load: 這裡的loaded表示unit檔案被systemd識别,并且加載到記憶體中了,一般這個都是loaded,除非這個unit檔案不存在;
  • ACTIVE:基本可以根據這個判斷
  • SUB:可以認為輸出是unit檔案中ExecStart所指定的腳本或者二進制指令目前的狀态,exit表示腳本執行完了,running表示或者指令還在執行
#systemctl list-units
  UNIT                                                              LOAD   ACTIVE SUB       DESCRIPTION
  rsyslog.service                                                   loaded active running   System Logging Service
  sshd.service                                                      loaded active running   OpenSSH server daemon
● staragentctl.service                                              loaded failed failed    SYSV: Staragent is a standard Monitoring UNIX program for xxxbaba
  syslog-ng.service                                                 loaded active running   System Logger Daemon
  sysstat.service                                                   loaded active exited    Resets System Activity Logs
  systemd-fsck-root.service                                         loaded active exited    File System Check on Root Device
  systemd-fsck@dev-disk-by\x2dlabel-\x5cx2fboot.service             loaded active exited    File System Check on /dev/disk/by-label/\x2fboot
  systemd-journal-flush.service                                     loaded active exited    Flush Journal to Persistent Storage
  systemd-journald.service                                          loaded active running   Journal Service
  systemd-logind.service                                            loaded active running   Login Service
  systemd-random-seed.service                                       loaded active exited    Load/Save Random Seed      

這個輸出,最需要注意的是static,經過測試,我對static的了解是:static表示一個service類型的unit檔案中,沒有Install區塊,意味着,這個unit檔案不能執行systemctl enable a

#systemctl list-unit-files
syslog-ng.service                      enabled
rsyncd.service                         disabled
sshd-keygen.service                    static      

比如:

#systemctl edit --full aa      
#systemctl cat aa
# /etc/systemd/system/aa.service
[Unit]
Description=aa
After=syslog.target network.target nss-lookup.target
Before=time-sync.target
Wants=time-sync.target
[Service]
Type=oneshot
ExecStart=/usr/libexec/aa
RemainAfterExit=yes      
#systemctl daemon-reload      
#systemctl restart aa      
#systemctl list-unit-files | grep aa
aa.service                             static      

staic檔案最讓人疑惑的一點是,現在A是static,B是enabled,然後B還是依賴A,現在A,B都是stop的狀态下,啟動B,A的/usr/libexec/aa 這個unit執行腳本或者指令會照常執行!!A雖然是static,但是static檔案存在的意義之一,就是為了解決别人的依賴。

systemd難以琢磨的依賴關系&systend對static unit行為方式

有人問我rc-local.service這個service在7u上有被其他的東西依賴嗎?其實,他還想問rc-local.service中的ExecStart是否會被執行?因為他知道rc-local.service 是一個static類型的unit檔案,是以他很迷惑,迷惑的是systemd中staic檔案的行為方式究竟是怎樣的?我這個問題大家可以自己先分析一下答案;

下面看我的思路:

如果你看完我後面測試,你應該得出如下了解:

  • 務必學會判斷一個service類型的unit檔案是的狀态是static,enable,disable?
  • 務必知道static的unit其實就是沒有[Install]區塊,但是[Unit] [Service]區塊肯定還是有的。
  • 如果A,B兩個unit,A和B都是enable的unit的話,reboot後,A,B的ExecStart肯定會被執行;
  • 如果A是static,B是enable,B After A,但是B中沒有說明B Requires A或者B Wants A,那麼A中的[Service]區塊中的ExecStart是否執行,取決與A自己;

比如rc-local.service,rc-local.service是一個staic的unit,注意看這個unit檔案的原生注釋,# This unit gets pulled automatically into multi-user.target by systemd-rc-local-generator if /etc/rc.d/rc.local is executable. 是以rc-local.service 中的ExecStart=/etc/rc.d/rc.local start 肯定會執行!因為,有systemd-rc-local-generator将 rc-local.service推到了multi-user.target中!!

#systemctl cat rc-local.service
# /usr/lib/systemd/system/rc-local.service
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes      

可是我還是不服,于是,我自己寫了2個unit aa bb,雖然我在aa的unit中寫了ConditionFileIsExecutable=/usr/libexec/aa ,并且確定 /usr/libexec/aa 可執行,但是,當我 reboot後,ExecStart=/usr/libexec/aa start還是沒有執行!也許systemd unit的行為方式令我們費解,但是,隻有不斷自己去測試,我們才能摸清,它的行為方式;

static檔案aa

[root@localhost /home/ahao.mah]
#systemctl cat aa
# /etc/systemd/system/aa.service
[Unit]
Description=aa
ConditionFileIsExecutable=/usr/libexec/aa
Before=bb.service
[Service]
Type=simple
ExecStart=/usr/libexec/aa start
RemainAfterExit=yes      

enable檔案bb

[root@localhost /home/ahao.mah]
#systemctl cat bb
# /etc/systemd/system/bb.service
[Unit]
Description=bb
After=syslog.target network.target nss-lookup.target aa.service
[Service]
Type=simple
ExecStart=/usr/libexec/bb
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target      

重新開機

[root@localhost /home/ahao.mah]
#systemctl reboot      

aa unit沒有啟動

[root@localhost /home/ahao.mah]
#systemctl status aa
● aa.service - aa
   Loaded: loaded (/etc/systemd/system/aa.service; static; vendor preset: disabled)
   Active: inactive (dead)      

bb unit已經啟動

[root@localhost /home/ahao.mah]
#systemctl status bb
● bb.service - bb
   Loaded: loaded (/etc/systemd/system/bb.service; enabled; vendor preset: disabled)
   Active: active (exited) since Fri 2016-08-19 10:52:15 CST; 2min 36s ago
  Process: 1046 ExecStart=/usr/libexec/bb (code=exited, status=0/SUCCESS)
 Main PID: 1046 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/bb.service
Aug 19 10:52:15 localhost.localdomain systemd[1]: Started bb.      

啟動aa unit

#systemctl start aa      

啟動aa unit成功

[root@localhost /home/ahao.mah]
#systemctl status aa
● aa.service - aa
   Loaded: loaded (/etc/systemd/system/aa.service; static; vendor preset: disabled)
   Active: active (exited) since Fri 2016-08-19 10:54:51 CST; 2s ago
  Process: 3837 ExecStart=/usr/libexec/aa start (code=exited, status=0/SUCCESS)
 Main PID: 3837 (code=exited, status=0/SUCCESS)
Aug 19 10:54:51 localhost systemd[1]: Started aa.
Aug 19 10:54:51 localhost systemd[1]: Starting aa...      

怎麼判斷有哪些unit依賴我

如果你初接觸systemd,那麼你很快會從網絡上知道,有下面這三種方法去判斷有什麼依賴一個unit

  • 我用#systemctl list-dependencies aa.service 去判斷
  • 我用#systemctl cat rc-local.service 去判斷
  • 我在/usr/lib/systemd目錄下,用#grep -nR ‘rc-local.service’ ./* 去判斷
  1. 你以為第一種方法可以輸出那些依賴aa.service的unit嗎?答案是,這個輸出并不是那麼好看!
systemctl list-dependencies aa.service --after 輸出的是依賴aa.service
和unit中有After=aa.service 的所有unit檔案
systemctl list-dependencies aa.service --before 輸出unit中
有Before=aa.service 的所有unit檔案      
  1. systemctl cat rc-local.service看不到什麼依賴它
  2. grep -nR ‘rc-local.service’ /usr/lib/systemd/* 你發現所有的unit中都沒有寫,自己Requirs或者Wants rc-local.service ,而是,大家都是Afer rc-local.service,After 不意味着依賴它,僅僅是,如果rc-local.service 這個unit要啟動,那麼我會在rc-local.service啟動後,我再啟動;
#grep -nR 'rc-local.service' /usr/lib/systemd/*
/usr/lib/systemd/system/[email protected]:13:After=rc-local.service
/usr/lib/systemd/system/plymouth-quit-wait.service:3:After=rc-local.service plymouth-start.service systemd-user-sessions.service
grep: /usr/lib/systemd/system/dbus-org.freedesktop.network1.service: No such file or directory
/usr/lib/systemd/system/multi-user.target.wants/plymouth-quit-wait.service:3:After=rc-local.service plymouth-start.service systemd-user-sessions.service
/usr/lib/systemd/system/multi-user.target.wants/plymouth-quit.service:3:After=rc-local.service plymouth-start.service systemd-user-sessions.service
/usr/lib/systemd/system/[email protected]:14:After=rc-local.service
/usr/lib/systemd/system/plymouth-quit.service:3:After=rc-local.service plymouth-start.service systemd-user-sessions.service
/usr/lib/systemd/system/console-getty.service:13:After=rc-local.service
/usr/lib/systemd/system/[email protected]:13:After=rc-local.service
/usr/lib/systemd/system/console-shell.service:12:After=rc-local.service
/usr/lib/systemd/system/[email protected]:12:After=rc-local.service
Binary file /usr/lib/systemd/system-generators/systemd-rc-local-generator matches      

target的依賴

其實我們知道一個target是一組unit的集合,要達到一個target需要執行這個target所包含的所有的unit,最常見的target是multi-user.target,arget依賴的unit都會放在一個目錄裡,如下,然而,如果B是一個service unit,B依賴A service,B不會建立一個目錄來存放自己所有的依賴的,因為,畢竟service和target待遇還是有差別的!

#ll /etc/systemd/system/multi-user.target.wants/
total 0
lrwxrwxrwx 1 root root 30 Aug 19 10:07 bb.service -> /etc/systemd/system/bb.service
lrwxrwxrwx 1 root root 37 Mar 21 15:36 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx 1 root root 38 Mar 21 15:58 docker.service -> /usr/lib/systemd/system/docker.service
lrwxrwxrwx 1 root root 36 Aug 16 03:01 ipmi.service -> /usr/lib/systemd/system/ipmi.service
lrwxrwxrwx 1 root root 38 Mar 22 03:00 mcelog.service -> /usr/lib/systemd/system/mcelog.service
lrwxrwxrwx 1 root root 39 Aug 18 17:55 ntpdate.service -> /usr/lib/systemd/system/ntpdate.service
lrwxrwxrwx 1 root root 36 Mar 21 15:36 ntpd.service -> /usr/lib/systemd/system/ntpd.service
lrwxrwxrwx 1 root root 39 Jun  1 09:30 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx 1 root root 36 Mar 21 15:36 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx 1 root root 41 Jul 28 17:12 syslog-ng.service -> /usr/lib/systemd/system/syslog-ng.service
lrwxrwxrwx 1 root root 39 Mar 21 15:36 sysstat.service -> /usr/lib/systemd/system/sysstat.service      

假設,我有兩個unit A,B,A是一個static檔案,B是一個enable的檔案 那麼,B依賴A的話,那麼reboot後,A中的ExecStart是否執行?如果B,僅僅是AfterA,那麼reboot後,A中的ExecStart是否執行?

自己測試

下面,我自己測試一下,首先建立兩個service:aa ,bb, 僅僅在bb的unit中寫bb 需要after bb;那麼

情況1

情況1:當bb沒有被enable(這意味着bb一定要有[Install]區塊,這裡我們肯定寫WantedBy=multi-user.target,但是bb在/etc/systemd/system/multi-user.target.wants/目錄下沒有軟連結),aa沒有Install區塊的時候(這是aa肯定是一個static檔案)

#systemctl status aa
● aa.service - aa
   Loaded: loaded (/etc/systemd/system/aa.service; static; vendor preset: disabled)
   Active: inactive (dead)      
#systemctl status bb
● bb.service - bb
   Loaded: loaded (/etc/systemd/system/bb.service; disabled; vendor preset: disabled)
   Active: inactive (dead)      
#systemctl is-enabled bb
disabled      
#ll /etc/systemd/system/multi-user.target.wants/
total 0
lrwxrwxrwx 1 root root 37 Mar 21 15:36 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx 1 root root 38 Mar 21 15:58 docker.service -> /usr/lib/systemd/system/docker.service
lrwxrwxrwx 1 root root 36 Aug 16 03:01 ipmi.service -> /usr/lib/systemd/system/ipmi.service
lrwxrwxrwx 1 root root 38 Mar 22 03:00 mcelog.service -> /usr/lib/systemd/system/mcelog.service
lrwxrwxrwx 1 root root 39 Aug 18 17:55 ntpdate.service -> /usr/lib/systemd/system/ntpdate.service
lrwxrwxrwx 1 root root 36 Mar 21 15:36 ntpd.service -> /usr/lib/systemd/system/ntpd.service
lrwxrwxrwx 1 root root 39 Jun  1 09:30 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx 1 root root 36 Mar 21 15:36 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx 1 root root 41 Jul 28 17:12 syslog-ng.service -> /usr/lib/systemd/system/syslog-ng.service
lrwxrwxrwx 1 root root 39 Mar 21 15:36 sysstat.service -> /usr/lib/systemd/system/sysstat.service      

情況2

情況2:當bb被enable的時候(這意味着bb一定要有Install區塊,這裡我們肯定寫WantedBy=multi-user.target,并且bb一定在/etc/systemd/system/multi-user.target.wants/目錄下有連結),aa沒有Install區塊的時候(這是aa肯定是一個static檔案)

#systemctl enable bb
Created symlink from /etc/systemd/system/multi-user.target.wants/bb.service to /etc/systemd/system/bb.service.      
#ll /etc/systemd/system/multi-user.target.wants/
total 0
lrwxrwxrwx 1 root root 30 Aug 19 10:07 bb.service -> /etc/systemd/system/bb.service
lrwxrwxrwx 1 root root 37 Mar 21 15:36 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx 1 root root 38 Mar 21 15:58 docker.service -> /usr/lib/systemd/system/docker.service
lrwxrwxrwx 1 root root 36 Aug 16 03:01 ipmi.service -> /usr/lib/systemd/system/ipmi.service
lrwxrwxrwx 1 root root 38 Mar 22 03:00 mcelog.service -> /usr/lib/systemd/system/mcelog.service
lrwxrwxrwx 1 root root 39 Aug 18 17:55 ntpdate.service -> /usr/lib/systemd/system/ntpdate.service
lrwxrwxrwx 1 root root 36 Mar 21 15:36 ntpd.service -> /usr/lib/systemd/system/ntpd.service
lrwxrwxrwx 1 root root 39 Jun  1 09:30 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx 1 root root 36 Mar 21 15:36 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx 1 root root 41 Jul 28 17:12 syslog-ng.service -> /usr/lib/systemd/system/syslog-ng.service
lrwxrwxrwx 1 root root 39 Mar 21 15:36 sysstat.service -> /usr/lib/systemd/system/sysstat.service      

輸出結果是

#systemctl status aa
● aa.service - aa
   Loaded: loaded (/etc/systemd/system/aa.service; static; vendor preset: disabled)
   Active: inactive (dead)      
#systemctl status bb
● bb.service - bb
   Loaded: loaded (/etc/systemd/system/bb.service; enabled; vendor preset: disabled)
   Active: active (exited) since Fri 2016-08-19 10:22:03 CST; 50s ago
  Process: 1044 ExecStart=/usr/libexec/bb (code=exited, status=0/SUCCESS)
 Main PID: 1044 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/bb.service
Aug 19 10:22:03 localhost.localdomain systemd[1]: Started bb.      
#cat /var/log/bb
Fri Aug 19 10:22:03 CST 2016      
#cat /var/log/aa
cat: /var/log/aa: No such file or directory      

四、target的了解

Targets are special unit files that describe a system state or synchronization point. Like other units, the files that define targets can be identified by their suffix, which in this case is .target. Targets do not do much themselves, but are instead used to group other units together.

Listing Available Targets

#systemctl list-unit-files --type=target     可以看到available targets,但是隻有狀态是active 的,才表示這個target被執行了;      

預設target

#systemctl get-default
multi-user.target      
#systemctl set-default graphical.target   設定預設target      

target相關指令

#systemctl list-dependencies basic.target --after輸出是什麼?
輸出的是,basic.target 這個target應該在其輸出的target運作之後運作!!!      
systemctl list-dependencies basic.target --before輸出是什麼?
輸出的是,basic.target 這個target應該在其輸出的target運作之前運作!!!      

特殊的multi-user.target

#systemctl list-dependencies multi-user.target --before
#systemctl list-dependencies multi-user.target --after      

你會發現multi-user.target 這個target是在很多基礎target搞定之後,才會運作multi-user.target,但是multi-user.target并不是最後一個運作的target,還有比他更晚的target,那就是graphical.target,在他後面運作的基本上不會被運作,紅色标記不僅說明了這一切!

#systemctl get-default
multi-user.target      
[root@localhost /home/ahao.mah]
#ll /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 37 Mar 18 16:48 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target      

更是說明了multi-user.target 是OS啟動過程中,最後一個要運作的target!!!

[root@localhost /home/ahao.mah]
#systemctl list-dependencies multi-user.target --before
multi-user.target
● ├─systemd-readahead-done.service
● ├─systemd-readahead-done.timer
● ├─systemd-update-utmp-runlevel.service
● └─graphical.target
●   └─systemd-update-utmp-runlevel.service      

五、參考

How To Use Systemctl to Manage Systemd Services and Units

systemd攻略之二:實戰unit,詳解static,target
systemd攻略之二:實戰unit,詳解static,target

繼續閱讀