linux 下的 service 和systemctl 服務管理方式
man service 得到:執行一個system V 風格的啟動腳本
service - run a System V init script
man systemctl 得到: 控制systemd 系統和服務管理器
systemctl - Control the systemd system and service manager
相關連接機官方文檔:
radhat 關於systemd 官方文檔:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/pdf/system_administrators_guide/Red_Hat_Enterprise_Linux-7-System_Administrators_Guide-en-US.pdf
手冊說明 有些官方找不到的參數這里查 :https://www.freedesktop.org/software/systemd/man/systemd.exec.html
init(為英語:initialization的簡寫)是 Unix 和 類Unix 系統中用來產生其它所有進程的程序。它以守護進程的方式存在,其進程號為1。Linux系統在引導時加載Linux內核后,便由Linux內核加載init程序,由init程序完成余下的引導過程,比如加載運行級別,加載服務,引導Shell/圖形化界面等等。
Unix 系列中(如 System III 和 System V)init的作用,和研究中的 Unix 和 BSD 派生版本相比,發生了一些變化。大多數Linux發行版是和 System V 相兼容的,但是一些發行版如Slackware 采用的是BSD風格,其它的如 Gentoo 是自己定制的。后來Ubuntu[1][2] 和其他一些發行版采用 Upstart[3] 來代替[4] 傳統的 init 進程。至2015年,大部分Linux發行版都已采用新的systemd替代System V和Upstart,但systemd向下兼容System V。
1.service
執行 service 命令就是在 /etc/init.d/ 執行可執行的sh 腳本,start|stop|restart 等命令參數都是腳本中定義好的函數
root@localhost init.d]# pwd
/etc/init.d
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# cat network
#! /bin/bash
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO
# Source function library.
. /etc/init.d/functions
2.systemctl
systemd是Linux系統最新的初始化系統(init),作用是提高系統的啟動速度,盡可能啟動較少的進程,盡可能更多進程並發啟動。
systemd對應的進程管理命令是systemctl, systemctl 也兼容了 service 功能
表 10.1. 可用的systemd單位類型
Unit Type | File Extension | Description |
---|---|---|
Service unit | .service |
A system service. |
Target unit | .target |
A group of systemd units. |
Automount unit | .automount |
A file system automount point. |
Device unit | .device |
A device file recognized by the kernel. |
Mount unit | .mount |
A file system mount point. |
Path unit | .path |
A file or directory in a file system. |
Scope unit | .scope |
An externally created process. |
Slice unit | .slice |
A group of hierarchically organized units that manage system processes. |
Snapshot unit | .snapshot |
A saved state of the systemd manager. |
Socket unit | .socket |
An inter-process communication socket. |
Swap unit | .swap |
A swap device or a swap file. |
Timer unit | .timer |
A systemd timer. |
表10.2。系統單元文件位置
目錄 | 描述 |
---|---|
/usr/lib/systemd/system/ |
使用已安裝的RPM軟件包分發的系統單元文件。 |
/run/systemd/system/ |
在運行時創建的系統單元文件。此目錄優先於已安裝服務單元文件的目錄。 |
/etc/systemd/system/ |
創建的系統單元文件systemctl enable 以及為擴展服務而添加的單元文件。此目錄優先於具有運行時單元文件的目錄。 |
通過該RPM 方式安裝的軟件包的unit 文件保存目錄: /usr/lib/systemd/system/
#目錄下包含許多以 .service 結尾的文件
[root@localhost system]# cd /usr/lib/systemd/system/
[root@localhost system]# ls
arp-ethers.service reboot.target
auditd.service reboot.target.wants
autovt@.service remote-cryptsetup.target
basic.target remote-fs-pre.target
basic.target.wants remote-fs.target
blk-availability.service rescue.service
bluetooth.target rescue.target
brandbot.path rescue.target.wants
brandbot.service rhel-autorelabel-mark.service
chrony-dnssrv@.service rhel-autorelabel.service
chrony-dnssrv@.timer rhel-configure.service
chronyd.service rhel-dmesg.service
chrony-wait.service rhel-domainname.service
console-getty.service rhel-import-state.service
systemd系統和服務管理器提供以下主要功能:
systemd系統和服務管理器提供以下主要功能: Socket-based :基於套接字的激活 - 在引導時,systemd為支持此類激活的所有系統服務創建偵聽套接字,並在它們啟動后立即將套接字傳遞給這些服務。這不僅允許systemd並行啟動服務,而且還可以重新啟動服務,而不會丟失任何在不可用時發送給它的消息:相應的套接字仍然可訪問,所有消息都排隊。 Systemd :Systemd使用套接字單元進行基於套接字的激活。 Bus :基於總線的激活 - 在客戶端應用程序第一次嘗試與它們通信時,可以按需啟動使用D-Bus進行進程間通信的系統服務。Systemd使用 D-Bus服務文件進行基於總線的激活。 Device :基於設備的激活 - 當插入特定類型的硬件或可用時,可以按需啟動支持基於設備的激活的系統服務。Systemd使用設備單元進行基於設備的激活。 Path :基於路徑的激活 - 當特定文件或目錄更改其狀態時,可以按需啟動支持基於路徑的激活的系統服務。Systemd使用路徑單元進行基於路徑的激活。 Mount :掛載和自動掛載點管理 - Systemd監視並管理掛載和自動掛載點。Systemd使用裝載單元作為掛載點,使用自動掛載單元作為自動掛載點。 Aggressive parallelization:積極的並行化 - 由於使用基於套接字的激活,systemd可以在所有偵聽套接字到位后立即並行啟動系統服務。結合支持按需激活的系統服務,並行激活可顯着減少引導系統所需的時間。 Transactional unit activation logic :事務單元激活邏輯 - 在激活或停用單元之前,systemd計算其依賴關系,創建臨時事務,並驗證此事務是否一致。如果事務不一致,systemd會在報告錯誤之前自動嘗試更正它並從中刪除非必要的作業。 Backwards compatibility with SysV init: 向后兼容SysV init - Systemd支持SysV init腳本,如 Linux標准基本核心規范中所述,這簡化了系統服務單元的升級路徑。
一般我們常見的是這四種:
Service unit .service
Target unit .target
Device unit .device
Mount unit .mount
我們通過yum 安裝一個 apache 看下 unit 文件:
[root@localhost system]# ll http* -rw-r--r-- 1 root root 752 4月 24 09:44 httpd.service [root@localhost 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 [root@localhost system]#
整個文件分三個部分,[Unit]·[Service]·[Install]
[Unit]:記錄unit文件的通用信息和控制。
[Service]:記錄Service的信息
[Install]:安裝信息。
1.定義控制單元 [Unit]
- 在 Systemd 中,所有引導過程中 Systemd 要控制的東西都是一個單元。基本的用法如下:
- Description:代表整個單元的描述,可根據需要任意填寫。
- Wants:本單元啟動了,它“想要”的單元也會被啟動。但是這個單元若啟動不成功,對本單元沒有影響。
- Requires: 這個單元啟動了,那么它“需要”的單元也會被啟動; 它“需要”的單元被停止了,它自己也活不了。但是請注意,這個設定並不能控制啟動順序,因為它“需要”的單元啟動也需要時間,若它“需要”的單元啟動還未完成,就開始啟動本單元,則本單元也無法啟動,所以不建議使用這個字段。
- OnFailure:若本單元啟動失敗了,那么啟動這個單元作為折衷。
- Before/After:指定啟動順序。
[Unit] #Unit 文件通用信息 Description=The Apache HTTP Server #描述 After=network.target remote-fs.target nss-lookup.target #表示在這些文件啟動后才能夠啟動,還有Before 表示在哪些文件啟動前啟動 Documentation=man:httpd(8) #手冊信息 Documentation=man:apachectl(8) #手冊信息
2.定義服務本體 [service]
[Service] Type=notify #服務的類型 EnvironmentFile=/etc/sysconfig/httpd #指定環境列表的文件 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND #這個參數是幾乎每個 .service 文件都會有的,指定服務啟動的主要命令,在每個配置文件中只能使用一次。 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 # 讓一個停止(stopped)的進程繼續執行. 本信號不能被阻塞. 可以用一個handler來讓程序在由stopped狀態變為繼續執行時完成特定的工作. 例如, 重新顯示提示符額,優雅的停止 PrivateTmp=true #采用布爾參數。如果為true,則為執行的進程設置新的文件系統名稱空間,並在其中安裝不由名稱空間外的進程共享的私有/tmp
和/var/tmp
目錄。這對於保護對進程的臨時文件的訪問非常有用
3.[Install]段
Alias為單元提供一個空間分離的附加名字。
RequiredBy單元被允許運行需要的一系列依賴單元,RequiredBy列表從Require獲得依賴信息。
WantBy單元被允許運行需要的弱依賴性單元,Wantby從Want列表獲得依賴信息。
Also指出和單元一起安裝或者被協助的單元。
DefaultInstance實例單元的限制,這個選項指定如果單元被允許運行默認的實例。
[Install] WantedBy=multi-user.target #系統啟動需要用戶級別3
#multi-user.target是systemV世界中運行級別3的替代方案
小測試:
執行systemctl enable apache.service 時 默認會去/usr/lib/systemd/system/ 目錄下找文件,直接放在 /etc/systemd/system/ 目錄下不行
[root@localhost system]# ll apache.service -rw-r--r-- 1 root root 672 7月 2 05:12 apache.service [root@localhost system]# systemctl enable apache.service #執行 systemctl enable 命令 后會自動創建一個鏈接指向 /etc/systemd/system/ 目錄下 Created symlink from /etc/systemd/system/multi-user.target.wants/apache.service to /usr/lib/systemd/system/apache.service. [root@localhost system]# systemctl start apache.service #啟動 [root@localhost system]# systemctl status apache.service #狀態 ● apache.service - The Apache HTTP Server Unit test Loaded: loaded (/usr/lib/systemd/system/apache.service; enabled; vendor preset: disabled) Active: active (running) since 二 2019-07-02 05:13:15 EDT; 7s ago Main PID: 17799 (httpd) Tasks: 6 Memory: 2.7M CGroup: /system.slice/apache.service ├─17799 /usr/sbin/httpd -DFOREGROUND ├─17800 /usr/sbin/httpd -DFOREGROUND ├─17801 /usr/sbin/httpd -DFOREGROUND ├─17802 /usr/sbin/httpd -DFOREGROUND ├─17803 /usr/sbin/httpd -DFOREGROUND └─17804 /usr/sbin/httpd -DFOREGROUND 7月 02 05:13:15 localhost.localdomain systemd[1]: Started The Apache HTTP Server Unit test. 7月 02 05:13:16 localhost.localdomain httpd[17799]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive global...s this message Hint: Some lines were ellipsized, use -l to show in full. *****************************************************************************************
這個文件是修改了 httpd.service 文件得來,刪除了一點東西
[root@localhost system]# cat apache.service [Unit] Description=The Apache HTTP Server Unit test After=network.target [Service] Type=simple 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 [root@localhost system]# pwd /usr/lib/systemd/system [root@localhost system]#