systemctl是RHEL 7 的服務管理工具中主要的工具,它融合之前service和chkconfig的功能於一體。可以使用它永久性或只在當前會話中啟用/禁用服務。
1.服務權限
systemd有系統和用戶區分;系統(/user/lib/systemd/system/)、用戶(/etc/lib/systemd/user/).
一般系統管理員手工創建的單元文件建議存放在/etc/systemd/system/目錄下面。
2.創建服務文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
|
[Unit]
Description : 服務的簡單描述
Documentation : 服務文檔
Before、After:定義啟動順序。Before=xxx.service,代表本服務在xxx.service啟動之前啟動。After=xxx.service,代表本服務在xxx.service之后啟動。
Requires:這個單元啟動了,它需要的單元也會被啟動;它需要的單元被停止了,這個單元也停止了。
Wants:推薦使用。這個單元啟動了,它需要的單元也會被啟動;它需要的單元被停止了,對本單元沒有影響。
[Service]
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會等待所有任務(Jobs)處理完成后,才開始執行idle類型的單元。除此之外,其他行為和Type=simple 類似。
PIDFile:pid文件路徑
ExecStart:指定啟動單元的命令或者腳本,ExecStartPre和ExecStartPost節指定在ExecStart之前或者之后用戶自定義執行的腳本。Type=oneshot允許指定多個希望順序執行的用戶自定義命令。
ExecReload:指定單元停止時執行的命令或者腳本。
ExecStop:指定單元停止時執行的命令或者腳本。
PrivateTmp:True表示給服務分配獨立的臨時空間
Restart:這個選項如果被允許,服務重啟的時候進程會退出,會通過systemctl命令執行清除並重啟的操作。
RemainAfterExit:如果設置這個選擇為真,服務會被認為是在激活狀態,即使所以的進程已經退出,默認的值為假,這個選項只有在Type=oneshot時需要被配置。
[Install]
Alias:為單元提供一個空間分離的附加名字。
RequiredBy:單元被允許運行需要的一系列依賴單元,RequiredBy列表從Require獲得依賴信息。
WantBy:單元被允許運行需要的弱依賴性單元,Wantby從Want列表獲得依賴信息。
Also:指出和單元一起安裝或者被協助的單元。
DefaultInstance:實例單元的限制,這個選項指定如果單元被允許運行默認的實例。
3.重載服務
systemctl enable nginx.service(設置開機自啟)
就會在/etc/systemd/system/multi-user.target.wants/目錄下新建一個/usr/lib/systemd/system/nginx.service 文件的鏈接。
4.操作服務
#啟動服務
$ sudo systemctl start nginx.service
#查看日志
$ sudo journalctl -f -u nginx.service
— Logs begin at 四 2015-06-25 17:32:20 CST. —
6月 25 10:28:24 Leco.lan systemd[1]: Starting nginx – high performance web server…
6月 25 10:28:24 Leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
6月 25 10:28:24 Leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful
6月 25 10:28:24 Leco.lan systemd[1]: Started nginx – high performance web server.
#重啟
$ sudo systemctl restart nginx.service
#重載
$ sudo systemctl reload nginx.service
#停止
$ sudo systemctl stop nginx.service
|
PS:使用命令 systemctl is-enabled postfix.service 得到的值可以是enable、disable或static,這里的 static 它是指對應的 Unit 文件中沒有定義[Install]區域,因此無法配置為開機啟動服務。
剛剛配置的服務需要讓systemctl能識別,就必須刷新配置
|
$ systemctl daemon-reload
|
如果沒有權限可以使用sudo
|
$
sudo
systemctl daemon-reload
|
啟動一個服務:systemctl start nginx.service
關閉一個服務:systemctl stop
postfix
.service
重啟一個服務:systemctl restart
nginx
.service
顯示一個服務的狀態:systemctl status
postfix
.service
在開機時啟用一個服務:systemctl enable
nginx
.service
在開機時禁用一個服務:systemctl disable
nginx
.service
查看服務是否開機啟動:systemctl is-enabled
nginx
.service
查看已啟動的服務列表:systemctl list-unit-files|grep enabled
查看啟動失敗的服務列表:systemctl --failed
/etc/systemd/system和/lib/systemd/system的區別
1. 區別
1.1 [/usr]/lib/systemd/system/ (軟件包安裝的單元)
The expectation is that /lib/systemd/system
is a directory that should only contain systemd unit files which were put there by the package manager (YUM/DNF/RPM/APT/etc).
1.2 /etc/systemd/system/(系統管理員安裝的單元, 優先級更高)
Files in /etc/systemd/system
are manually placed here by the operator of the system for ad-hoc software installations that are not in the form of a package. This would include tarball type software installations or home grown scripts.
2. 優先級
systemd的使用大幅提高了系統服務的運行效率, 而unit的文件位置一般主要有三個目錄:
1 |
Table 1. Load path when running in system mode (--system). |
這三個目錄的配置文件優先級依次從高到低,如果同一選項三個地方都配置了,優先級高的會覆蓋優先級低的。
系統安裝時,默認會將unit文件放在/lib/systemd/system
目錄。如果我們想要修改系統默認的配置,比如nginx.service
,一般有兩種方法:
- 在
/etc/systemd/system
目錄下創建nginx.service
文件,里面寫上我們自己的配置。 - 在
/etc/systemd/system
下面創建nginx.service.d
目錄,在這個目錄里面新建任何以.conf結尾的文件,然后寫入我們自己的配置。推薦這種做法。
/run/systemd/system
這個目錄一般是進程在運行時動態創建unit文件的目錄,一般很少修改,除非是修改程序運行時的一些參數時,即Session級別的,才在這里做修改。