使用systemd 指令碼設定服務keepalive
技術標籤:Linux
一. 適用場景
- 長時間執行, 且不能停止的任務,如 nginx/maxwell
- 任務異常停止, 需要可以立即按預設時間進行自啟動
二. systemd 簡介
systemd是Linux下的一種init軟體,由Lennart Poettering帶頭開發,其開發目標是提供更優秀的框架以表示系統服務間的依賴關係,並依此實現系統初始化時服務的並行啟動,同時達到降低Shell的系統開銷的效果,最終代替現在常用的System V與BSD風格init程式。傳統sysvinit使用inittab來決定執行哪些shell指令碼,大量使用shell指令碼被認為是效率低下無法並行的原因。systemd使用了Linux專屬技術,不再顧及POSIX相容.
三. 配置樣例
配置檔案目錄: /etc/systemd/system/
vim /etc/systemd/system/binlogdata_to_kafka_test.service
[Unit] Description=maxwell binlogdata_to_kafka_test Wants=network-online.target After=network-online.target [Service] Type=simple ExecStart=/data/maxwell/bin/maxwell --config /data/maxwell/binlogdata_to_kafka_test.properties WorkingDirectory=/data/maxwell/ StandardOutput=inherit StandardError=inherit Restart=always RestartSec=20 User=hdfs StartLimitIntervalSec=0 [Install] WantedBy=multi-user.target
重要引數說明:
①: StartLimitIntervalSec: 設定單元的啟動頻率限制。 StartLimitIntervalSec= 用於設定時長, 預設值等於 DefaultStartLimitIntervalSec= 的值(預設為10秒),設為 0 表示不作限制。
②: 如果要以90秒的間隔, 重新啟動服務3次, 可以在systemd服務檔案的section塊[Service]中包含以下行:
Restart=always
RestartSec=90
StartLimitInterval=300
StartLimitBurst=3
注意: 請注意,’StartLimitInterval’必須大於’RestartSec * StartLimitBurst’,否則服務將無限期地重新啟動.
四. 其他設定
4.1 *.service配置檔案修改後,需重新載入新的配置檔案,再啟動
systemctl daemon-reload
4.2 [開啟 | 禁用 ]服務開機自啟動
systemctl [ enable | disable | is-enabled | is-active ] xxxx.service
4.3 [啟動 | 停止 | 重啟] 服務
systemctl [ start | stop | restart ] xxxx.service
4.3 檢視服務狀態
systemctl status xxxx.service
4.4 列出所有的服務list
systemctl list-units --type=service
[[email protected] system]# systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
atd.service loaded active running Job spooling tools
auditd.service loaded active running Security Auditing Service
cloud-config.service loaded active exited Apply the settings specified in cloud-conf
cloud-final.service loaded active exited Execute cloud user/final scripts
cloud-init-local.service loaded active exited Initial cloud-init job (pre-networking)
cloud-init.service loaded active exited Initial cloud-init job (metadata service c
cloudera-scm-agent.service loaded active exited LSB: Cloudera SCM Agent
● cloudResetPwdAgent.service loaded failed failed LSB: @[email protected]
cloudResetPwdUpdateAgent.service loaded active running LSB: @[email protected]
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
[email protected] loaded active running Getty on tty1
● HSSInstall.service not-found active exited HSSInstall.service
irqbalance.service loaded active running irqbalance daemon
kdump.service loaded active exited Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required static device node
binlogdata_to_kafka_test.service loaded active running maxwell binlogdata_to_kafka_test
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
17 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
4.5 systemd的 unit配置檔案位置(redhat /centos)
在 /usr/lib/systemd/system/下面
enable命令 是在/etc/systemd/system/multi-user.target.wants 下做的指向unit目錄的(一般為/usr/lib/systemd/system/, 也可以在/etc/systemd/system/下)軟連結,如下:
[[email protected] multi-user.target.wants]# ll
total 0
lrwxrwxrwx. 1 root root 35 Feb 27 2019 atd.service -> /usr/lib/systemd/system/atd.service
lrwxrwxrwx. 1 root root 38 Feb 27 2019 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx 1 root root 58 Dec 21 13:47 binlogdata_to_kafka_test.service -> /etc/systemd/system/binlogdata_to_kafka_test.service