天天看點

[centos7]自啟動服務設定說明及nginx開機自啟動設定基礎環境注冊服務附(參考配置)

基礎環境

OS:CentOS7

Ngninx: 1.19.7

注冊服務

安裝服務

CentOS服務常用目錄:/lib/systemd/system/

一般系統啟動服務來源于上面這個目錄,服務對應檔案名,以".service"結尾

# 切換至服務存儲目錄
cd /lib/systemd/system/
# 建立服務,注意檔案名即服務名 ,也可以用vi/vim直接編輯檔案(沒有會自動建立)
touch nginx.service

           

nginx.server服務内容(其中路徑為預設安裝路徑,建議根據實際進行修改)

[Unit]
Description=nginx service
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target
           

配置說明:

[Unit]服務的說明

Description: 描述服務,還一個Documentation字段用于給出文檔位置

After: 描述服務啟動順序,還有一個Before字段,定義應該在哪些服務之前啟動

注意,After和Before字段隻涉及啟動順序,不涉及依賴關系

另外還有兩個可能用到的字段

Wants: 表示與nginx服務之間存在"弱依賴"關系,即如果依賴服務啟動失敗或停止運作,不影響nginx服務的繼續執行。

Requires: 字段則表示"強依賴"關系,即如果該服務啟動失敗或異常退出,那麼nginx服務也必須退出。

注意,Wants字段與Requires字段隻涉及依賴關系,與啟動順序無關,預設情況下是同時啟動的。

[Service]服務運作參數的設定

EnvironmentFile字段:指定目前服務的環境參數檔案。該檔案内部的key=value鍵值對,可以用$key的形式,在目前配置檔案中擷取,如啟動指令中:

ExecStart=/usr/sbin/xxx -D $OPTIONS

Type: 可選值如下:

simple(預設值):ExecStart字段啟動的程序為主程序

forking:ExecStart字段将以fork()方式啟動,此時父程序将會退出,子程序将成為主程序

oneshot:類似于simple,但隻執行一次,Systemd 會等它執行完,才啟動其他服務

dbus:類似于simple,但會等待 D-Bus 信号後啟動

notify:類似于simple,啟動結束後會發出通知信号,然後 Systemd 再啟動其他服務

idle:類似于simple,但是要等到其他任務都執行完,才會啟動該服務。一種使用場合是為讓該服務的輸出,不與其他服務的輸出相混合

ExecStart: 啟動指令

ExecReload: 重新開機指令

ExecStop: 停止指令

PrivateTmp: True表示給服務配置設定獨立的臨時空間

ExecStartPre字段:: 啟動服務之前執行的指令

ExecStartPost字段:: 啟動服務之後執行的指令

ExecStopPost字段:: 停止服務之後執行的指令

.

KillMode:

control-group(預設值):目前控制組裡面的所有子程序,都會被殺掉

process:隻殺主程序

mixed:主程序将收到 SIGTERM 信号,子程序收到 SIGKILL 信号

none:沒有程序會被殺掉,隻是執行服務的 stop 指令。

.

Restart:

no(預設值):退出後不會重新開機

on-success:隻有正常退出時(退出狀态碼為0),才會重新開機

on-failure:非正常退出時(退出狀态碼非0),包括被信号終止和逾時,才會重新開機

on-abnormal:隻有被信号終止和逾時,才會重新開機

on-abort:隻有在收到沒有捕捉到的信号終止時,才會重新開機

on-watchdog:逾時退出,才會重新開機

always:不管是什麼退出原因,總是重新開機

.

RestartSec:

重新開機服務之前,需要等待的秒數

注意:

[Service]的啟動、重新開機、停止指令需使用完整路徑

[Install]服務安裝設定(如何設定開機啟動)

***WantedBy:***表示該服務所在的服務組,常見服務組如下:

multi-user.target: 開機啟動

shutdown.target: 關機

其他常用指令

# 擷取目前服務組
systemctl get-default
# 重新加載配置檔案
systemctl daemon-reload nginx
# 啟動nginx服務
systemctl start nginx
# 停止服務
systemctl stop nginx
# 重新啟動服務
systemctl restart nginx
# 檢視所有已啟動的服務
systemctl list-units --type=service     
# 檢視服務目前狀态
systemctl status nginx
# 設定開機自啟動
systemctl enable nginx
# 取消開機自啟動
systemctl disable nginx.service         
           

附(參考配置)

Docker預設安裝後的服務配置如下:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target
           

繼續閱讀