systemctl的操作詳解總結及其與service的區別

lusklusklusk發表於2023-09-25

Redhat linux系列的centos作業系統和Debian linux系列的ubuntu作業系統均如此


總結:
1、systemctl就是集service和chkconfig一體的功能,systemctl enable就是類似chkconfig,只要把文字型別的可執行檔案放入/usr/lib/systemd/system/就能實現systemctl的操作,執行systemctl enable XX就是把XX加入開機啟動,即透過/usr/lib/systemd/system/XX建立軟連線/etc/systemd/system/multi-user.target.wants/XX
2、centos7之前的版本(不包括7),系統服務啟動停止以及狀態檢視的管理命令工具是service,啟動的第一個程式為init,init程式負責後面所有程式的啟動,但是透過init程式啟動的後續程式都是序列啟動的,啟動速度會慢一些(服務依靠shell指令碼順序啟動)。而chkconfig命令用於設定某個服務在系統某個執行級別是否自啟動,chkconfig可以理解為是一個用於維護/etc/rc[0-6].d目錄(小知識點此目錄下以S開頭的命令是start的意思,K開頭的命令是kill的意思)的命令列工具。service命令管理的服務其相關指令碼儲存目錄:/etc/init.d
centos7以後版本用systemd服務統一管理服務程式,對於服務的啟停以及開機自動啟動由命令systemctl統一管理,第一個程式名為為systemd,systemd採用並行啟動後續的程式而不是序列順序,因此啟動速度會比service更快一些,service屬於SysVinit(System V Init),systemctl屬於systemd,是SysVinit的繼任者,systemd借鑑了MAC OS系統的launchd思想。systemctl採用了target的模式,centos7的/etc/systemd/system/multi-user.target.wants命令列模式為multi-user.target可理解為centos6中runlevel 3的模式,centos7的/etc/systemd/system/graphical.target.wants命令列模式為graphical.target可理解為centos6中runlevel 5模式。如centos6需在命令列模式下開機自啟動的服務,則需要將對應服務指令碼關聯至runlevel 3啟動載入的目錄/etc/rc3.d下;而對應centos7中則是將對應服務指令碼關聯至對應的target目錄/etc/systemd/system/multi-user.target.wants下。systemctl命令管理的服務其相關指令碼儲存目錄:/usr/lib/systemd/system
3、/usr/lib/systemd/system目錄下的檔案不能使二進位制的可執行檔案,比如influxd為例子/usr/lib/systemd/system/influxd不能是二進位制的可執行檔案,只能是一個sh這樣的文字的可執行檔案,直接把二進位制檔案influxd加入/etc/init.d/無法正常service influxd start啟動,加入/usr/lib/systemd/system/也無法正常systemctl start influxd啟動,且influxdb.service服務裡面不能直接寫成ExecStart=/usr/bin/influxd &,systemctl start influxdb會報錯Error: unknown command "&" for "influxd";也不能寫成ExecStart=/usr/bin/influxd,systemctl start influxdb會不停的自動重啟,因為服務啟動時間過長,達到超時時間就自動結束了服務並不停的自動重啟然後出現報錯influxdb.service start operation timed out. Terminating。因為influxd沒有/usr/bin/influxd --help |grep daemonize或/usr/bin/influxd --help |grep quite這種後臺執行的引數,所以還是需要&讓它在後臺跑,但是&又無直接ExecStart=/usr/bin/influxd &,所以需要藉助“/bin/bash -c”將一個長字串當作一條完整的命令來執行,即寫成ExecStart=/bin/bash -c "/usr/bin/influxd &"
4、/lib/systemd/system和/usr/lib/systemd/system的區別,/lib目錄是軟連結名稱,/lib實際指向/usr/lib。
   /etc/systemd/system和/usr/lib/systemd/system的區別,/etc/systemd/system下面的檔案都是軟連結名稱,這些軟連結實際指向/lib/systemd/system目錄的對應檔案,/etc/systemd/system這個目錄有點像以前centos 6的/etc/rc.d/rc5.d/之類的功能,執行優先序比/usr/lib/systemd/system高
   /etc/systemd/system/multi-user.target.wants和/usr/lib/systemd/system的區別,/etc/systemd/system/multi-user.target.wants下面的檔案都是軟連結名稱,這些軟連結實際指向/usr/lib/systemd/system目錄的對應檔案
   /lib/systemd/system和/lib/systemd/user的區別,系統system和使用者user的區別
5、如果想讓ExecStart的服務遇到不正常的退出訊號時能夠自動重啟,則配置Restart=on-failure,如果想讓ExecStart的服務只要出現退出就自動重啟(除systemctl stop XX之外的其他所有退出動作比如主動kill掉後也能自動重啟),則配置Restart=always


啟動服務:systemctl start xxx.service
關閉服務:systemctl stop xxx.service
重啟服務:systemctl restart xxx.service
顯示服務的狀態:systemctl status xxx.service
在開機時啟用服務:systemctl enable xxx.service
在開機時禁用服務:systemctl disable xxx.service
檢視服務是否開機啟動:systemctl is-enabled xxx.service
檢視服務有哪些依賴項:systemctl list-dependencies xxx.service
檢視已啟動的服務列表:systemctl list-unit-files|grep enabled
檢視啟動失敗的服務列表:systemctl --failed
重新載入一個服務的配置檔案:systemctl reload xxx.service
過載所有修改過的配置檔案:systemctl daemon-reload
檢視systemctl服務相關日誌:journalctl -xe


設定開機自啟的命令即建立軟連線檔案將服務的管理指令碼檔案從服務目錄/usr/lib/systemd/system/下,建立軟連線至對應的target目錄/etc/systemd/system/multi-user.target.wants下;關閉開機自啟則是將對應服務指令碼的連結檔案從/etc/systemd/system/multi-user.target.wants/下刪除。

[root@FRSBachDEV2 ~]# systemctl enable influxdb
Created symlink from /etc/systemd/system/influxd.service to /usr/lib/systemd/system/influxdb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/influxdb.service to /usr/lib/systemd/system/influxdb.service.
[root@FRSBachDEV2 ~]# ll /etc/systemd/system/multi-user.target.wants |grep influxdb |grep -v grep
lrwxrwxrwx. 1 root root 40 Feb 23  2022 influxdb.service -> /usr/lib/systemd/system/influxdb.service
[root@FRSBachDEV2 ~]# systemctl disable influxdb
Removed symlink /etc/systemd/system/multi-user.target.wants/influxdb.service.
Removed symlink /etc/systemd/system/influxd.service.
[root@FRSBachDEV2 ~]# ll /etc/systemd/system/multi-user.target.wants |grep influxdb |grep -v grep






systemctl的啟動檔案存放在/usr/lib/systemd/system目錄下,檔案的模組主要分為Unit,Unit型別,Install這3個模組
1、[unit]
Unit本身的說明,以及與其他相依daemon的設定,包括在什麼服務之後才啟動此unit之類的設定值,Unit常用的選項:
可選項            描述
Description    對當前服務的簡單描述
After            可以指定在哪些服務之後進行啟動
Before            可以指定在哪些服務之前進行啟動
Requires    指定依賴於哪些服務(此依賴是”強依賴”,一旦所依賴的服務異常,當前的服務也隨之停止)
Wants            指定依賴於哪些服務(此依賴是”弱依賴”,所依賴的服務異常,不影響當前的服務)
Conflicts    定義units間的衝突關係
Wants=:Configures (weak) requirement dependencies on other units.However, if the listed units fail to start or cannot be added to the transaction, this has no impact on the validity of the transaction as a whole, and this unit will still be started.
Requires=:Similar to Wants=, but declares a stronger requirement dependency.If this unit gets activated, the units listed will be activated as well. If one of the other units fails to activate, and an ordering dependency After= on the failing unit is set, this unit will not be started. Besides, with or without specifying After=,this unit will be stopped if one of the other units is explicitly stopped.

2、[Service]
根據 unit 型別使用相對應的設定專案,拿service當範本,主要規範服務啟動的指令碼、環境配置檔案檔名、重新啟動的方式等等,systemctl的管理單位是unit,不僅僅管理service也管理其他比如socket、 device、mount、swap、target、path、timer、slice、scope,service只是systemctl的管理單位unit的一種,Service常用選項:
可選項            描述
Type            Configures the process start-up type for this service unit.
                simple:預設值,這個服務主要由 ExecStart 設定的程式來啟動,啟動後常駐於記憶體中。
                forking:由 ExecStart 指定的啟動的程式透過spawns產生子程式提供服務,然後父程式退出。
Environment    為服務新增環境變數
EnvironmentFile    指定當前服務啟動的環境變數配置檔案
ExecStart    指定服務啟動時執行的命令或指令碼,接受 "命令 引數 引數..." 的格式,不能接受 <, >, >>, |, & 等特殊字元,很多的bas語法也不支援。所以,要使用這些特殊的字元時,最好直接寫入到指令碼裡或藉助“/bin/bash -c”將一個長字串當作一條完整的命令來執行比如ExecStart=/bin/bash -c "/usr/bin/influxd &"
ExecStartPre    指定服務啟動前執行的命令或指令碼
ExecStartPost    指定服務啟動後執行的命令或指令碼
ExecStop    指明停止服務要執行的命令或指令碼
ExecStopPost    指定服務停止後執行的命令或指令碼
RestartSec    指定服務在重啟時等待的時間,單位為秒
ExecReload    指明重新載入服務要執行的命令或指令碼
Restart            Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached.配置當服務程式退出、被終止或超時時是否重新啟動服務
                no:退出後不會重啟
                on-success:當程式正常退出時(退出碼為0) 執行重啟
                on-failure:當程式不正常退出時(退出碼不為0) 執行重啟
                on-abnormal:當被訊號終止和超時執行重啟
                always:一直重啟
                on-abort:當收到沒有捕捉到的訊號終止時執行重啟
User            執行服務的使用者,會影響服務對本地檔案系統的訪問許可權
Group            執行服務的使用者組,會影響服務對本地檔案系統的訪問許可權
TimeoutStartSec    設定該服務允許的最大啟動時長,設為0,表示永不超時
Type =:If set to simple (the default if ExecStart= is specified but neither Type= nor BusName= are), the service manager will consider the unit started immediately after the main service process has been forked off. It is expected that the process configured with ExecStart= is the main process of the service. In this mode, if the process offers functionality to other processes on the system, its communication channels should be installed before the service is started up (e.g. sockets set up by systemd, via socket activation), as the service manager will immediately proceed starting follow-up units, right after creating the main service process, and before executing the service's binary. Note that this means systemctl start command lines for simple services will report success even if the service's binary cannot be invoked successfully (for example because the selected User= doesn't exist, or the service binary is missing).
如果設定為 simple(如果指定了 ExecStart= 但 Type= 和 BusName= 均未指定,則為預設值),服務管理器將認為該單元在主服務程式派生後立即啟動。預計ExecStart=配置的程式是服務的主程式。在此模式下,如果程式向系統上的其他程式提供功能,則應在服務啟動之前安裝其通訊通道(例如,由 systemd 透過套接字啟用設定套接字),因為服務管理器將立即開始執行以下操作-up 單元,在建立主服務程式之後、執行服務的二進位制檔案之前。請注意,這意味著即使無法成功呼叫服務的二進位制檔案(例如,因為所選的 User= 不存在,或者服務二進位制檔案丟失),簡單服務的 systemctl start 命令列也會報告成功。
Type =:If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main service process, and the service manager will consider the unit started when the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can reliably identify the main process of the service. systemd will proceed with starting follow-up units as soon as the parent process exits.
如果設定為 forking,則預計使用 ExecStart= 配置的程式將呼叫 fork() 作為其啟動的一部分。當啟動完成並且所有通訊通道都建立後,父程式預計將退出。子程式繼續作為主服務程式執行,當父程式退出時,服務管理器會認為該單元已啟動。這是傳統 UNIX 服務的行為。如果使用此設定,建議同時使用PIDFile=選項,以便systemd能夠可靠地識別服務的主程式。一旦父程式退出,systemd 將繼續啟動後續單元。
Restart:=If set to no (the default), the service will not be restarted. 如果設定為 no(預設值),則不會重新啟動該服務。
Restart:=If set to on-success, it will be restarted only when the service process exits cleanly.如果設定為on-success,只有當服務程式乾淨退出時才會重新啟動。
Restart:=If set to on-failure, the service will be restarted when the process exits with a non-zero exit code, is terminated by a signal (including on core dump, but excluding the aforementioned four signals), when an operation (such as service reload) times out, and when the configured watchdog timeout is triggered.如果設定為 on-failure,當程式以非零退出程式碼退出、被訊號終止(包括核心轉儲,但不包括上述四個訊號)、當某個操作(例如服務reload) 超時,以及觸發配置的看門狗超時,服務將重新啟動。
Restart:=If set to on-abnormal, the service will be restarted when the process is terminated by a signal (including on core dump, excluding the aforementioned four signals), when an operation times out, or when the watchdog timeout is triggered. 如果設定為on-abnormal,則當程式被訊號終止時(包括core dump,不包括上述四個訊號)、操作超時或觸發看門狗超時時,服務將重新啟動。
Restart:=If set to on-abort, the service will be restarted only if the service process exits due to an uncaught signal not specified as a clean exit status. 如果設定為 on-abort,則僅當服務程式由於未指定為乾淨退出狀態的未捕獲訊號而退出時,服務才會重新啟動。
Restart:=If set to always, the service will be restarted regardless of whether it exited cleanly or not, got terminated abnormally by a signal, or hit a timeout.如果設定為always,則無論服務是否乾淨退出、是否因訊號異常終止或超時,服務都將重新啟動。

3、[Install]
將 unit 安裝到那個target裡面去的意思,Install常用選項:
Install是服務的安裝資訊,它不在 systemd 的執行期間使用,只在使用 systemctl enable 和 systemctl disable 命令啟用/禁用服務時有用,所有 Unit 檔案通用,用來定義如何啟動,以及是否開機啟動,引數詳解如下:
WantedBy:這個unit附掛在哪一個 target unit 下面的,一般來說,大多的服務性質的 unit 都是附掛在 multi-user.target下面。它的值是一個或多個 target,執行enable命令時,符號連結會放入/etc/systemd/system目錄下以target 名 + .wants字尾構成的子目錄中。“WantedBy=multi-user.target” 表明當系統以多使用者方式(預設的執行級別)啟動時,這個服務需要被自動執行。當然還需要 systemctl enable 啟用這個服務以後自動執行才會生效
RequiredBy:依賴當前服務的模組。它的值是一個或多個 target,執行enable命令時,符號連結會放入/etc/systemd/system目錄下以 target 名 + .required字尾構成的子目錄中
Also:當前 Unit 被 enable/disable 時,會被同時操作的其他 Unit
Alias:當前 Unit 可用於啟動的別名




例子,配置datadomain儲存在ubuntu上開機自動掛載

root@DAILAPGDBUP001:~# cat /etc/issue
Ubuntu 22.04.1 LTS \n \l
root@DAILAPGDBUP001:~# cat /root/mountdatadomaindir.sh
/opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# vim /usr/lib/systemd/system/mountdatadomaindir.service
[Unit]
Description=mountdatadomaindir
After=network.target
[Service]
User=root
Group=root
Type=forking
ExecStart=/bin/bash /root/mountdatadomaindir.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
root@DAILAPGDBUP001:~# systemctl enable mountdatadomaindir
root@DAILAPGDBUP001:~# reboot
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.1G   65G  12% /
tmpfs                              7.8G  1.1M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  249M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  1.2G  149G   1% /PGDATA
boostfs                            377T  287T   90T  77% /mnt/datadomaindir
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
root@DAILAPGDBUP001:~# systemctl status mountdatadomaindir
● mountdatadomaindir.service - mountdatadomaindir
     Loaded: loaded (/lib/systemd/system/mountdatadomaindir.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-09-15 09:03:32 UTC; 2min 41s ago
    Process: 943 ExecStart=/bin/bash /root/mountdatadomaindir.sh (code=exited, status=0/SUCCESS)
   Main PID: 1091 (boostfs)
      Tasks: 4 (limit: 19009)
     Memory: 13.6M
        CPU: 80ms
     CGroup: /system.slice/mountdatadomaindir.service
             └─1091 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomain_archivelog -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# systemctl stop mountdatadomaindir
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.1G   65G  12% /
tmpfs                              7.8G     0  7.8G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  249M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  1.2G  149G   1% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
root@DAILAPGDBUP001:~# systemctl start mountdatadomaindir
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.1G   65G  12% /
tmpfs                              7.8G  1.1M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  249M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  1.2G  149G   1% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
boostfs                            377T  287T   90T  77% /mnt/datadomaindir



Restart=on-failure時,systemctl stop mountdatadomaindir後不會自動起來,kill也不會起來(/mnt/datadomaindir目錄不見了)

root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1806708       1  0 11:28 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# kill 1806708
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.2G   65G  12% /
tmpfs                              7.8G  5.5M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  251M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  6.4G  144G   5% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0



Restart=on-success時,systemctl stop mountdatadomaindir後不會自動起來,kill會自動起來(/mnt/datadomaindir目錄看見了)

root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1807834       1  0 11:34 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# kill 1807834
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.2G   65G  12% /
tmpfs                              7.8G  4.3M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  251M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  6.5G  144G   5% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
boostfs                            377T  302T   76T  81% /mnt/datadomaindir
root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1807932       1 10 11:35 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true





Restart=always時,systemctl stop mountdatadomaindir後不會自動起來,kill會自動起來(/mnt/datadomaindir目錄看見了)

root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1809107       1  3 11:41 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true
root@DAILAPGDBUP001:~# kill 1809107
root@DAILAPGDBUP001:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   77G  8.2G   65G  12% /
tmpfs                              7.8G  4.3M  7.8G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  251M  1.6G  14% /boot
/dev/mapper/vg--pgdb-lv--pgdb      150G  6.4G  144G   5% /PGDATA
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/0
boostfs                            377T  302T   76T  81% /mnt/datadomaindir
root@DAILAPGDBUP001:~# ps -ef|grep datadomain|grep -v grep
root     1809197       1  0 11:42 ?        00:00:00 /opt/emc/boostfs/bin/boostfs mount /mnt/datadomaindir -d DAILADD01.dai.netdai.com -s daipostgres -o allow-others=true


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30126024/viewspace-2985816/,如需轉載,請註明出處,否則將追究法律責任。

相關文章