systemctl 參數說明

轉載:https://blog.csdn.net/qq_29663071/article/details/80814081

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

 

例:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章