systemd 和 如何修改和建立一個 systemd service (Understanding and administering systemd)

xuyaowen發表於2018-08-16

系統中經常會使用到 systemctl 去管理systemd程式,剛剛看了一篇關於 systemd 和 SysV 相關的文章,這裡簡要記錄一下:

systemd定義: (英文來解釋更為原汁原味)

systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts. systemd provides:

  • Aggressive parallelization capabilities

  • Uses socket and D-Bus activation for starting services

  • Offers on-demand starting of daemons, keeps track of processes using Linux cgroups

  • Supports snapshotting and restoring of the system state

  • Maintains mount and automount points

  • Implements an elaborate transactional dependency-based service control logic.

The systemctl command is the primary tool to manage systemd. It combines the functionality of SysVinit’s service and chkconfig commands into a single tool you can use to enable and disable services permanently or only for the current session.

systemd manages units, which are representations of system resources and services. This following list shows the unit types that systemd can manage:

service

A service on the system, including instructions for starting, restarting, and stopping the service.

socket

A network socket associated with a service.

device

A device specifically managed with systemd.

mount

A mountpoint managed with systemd.

automount

A mountpoint automatically mounted on boot.

swap

Swap space on the system.

target

A synchronization point for other units. Usually used to start enabled services on boot.

path

A path for path-based activation. For example, you can start services based on the state of a certain path, such as whether it exists or not.

timer

A timer to schedule activation of another unit.

snapshot

A snapshot of the current systemd state. Usually used to rollback after making temporary changes to systemd.

slice

Restrivtion of resources through Linux Control Group nodes (cgroups).

scope

Information from systemd bus interfaces. Usually used to manage external system processes.

systemctl 常用管理命令:

# systemctl start foo
# systemctl stop foo
# systemctl restart foo
# systemctl status foo
# systemctl enable foo
# systemctl disable foo
# systemctl mask foo
# systemctl is-enabled foo

systemctl service 建立:

systemctl service在兩個目錄中: /etc/systemd/system 和 /usr/lib/systemd/system 之中

我們一般自己建立的service 直接放在 /etc/systemd/system 之中即可:

vim xuyaowen.service 建立檔案

輸入如下內容:

[Unit]
Description=xuyaowen custom service
Requires=network.target

[Service]
Type=simple
ExecStart=/usr/bin/sleep infinity

[Install]
WantedBy=multi-user.target

儲存退出後,使用 systemctl start xuyaowen 啟動後,使用 systemctl status 檢視service 狀態:

➜  system systemctl status xuyaowen.service    
● xuyaowen.service - xuyaowen custom service
   Loaded: loaded (/etc/systemd/system/xuyaowen.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-08-16 10:44:05 CST; 38min ago
 Main PID: 19556 (sleep)
    Tasks: 1 (limit: 4915)
   Memory: 172.0K
   CGroup: /system.slice/xuyaowen.service
           └─19556 /usr/bin/sleep infinity

Aug 16 10:44:05 yaowenxu systemd[1]: Started xuyaowen custom service.

service 建立成功。具體systemd 與 SysV 之間的關係可以在網路上較為容易查詢到,我這裡只是作為簡要記錄。關於systemd 更為詳細的內容,請參考 fedora doc .


 保持更新,轉載請註明出處。如果您覺得本文對你有幫助請點右下角推薦,謝謝

相關文章