使用systemd 指令碼設定服務keepalive

2021-10-12 12:47:38 字數 4630 閱讀 6474

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=******

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』,否則服務將無限期地重新啟動.

systemctl daemon-reload

systemctl [ enable | disable | is-enabled | is-active ] ***x.service

systemctl [ start | stop | restart ] ***x.service

systemctl status ***x.service

systemctl list-units --type=service

[root@cdh1 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-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

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'.

在 /usr/lib/systemd/system/下面

enable命令 是在/etc/systemd/system/multi-user.target.wants 下做的指向unit目錄的(一般為/usr/lib/systemd/system/, 也可以在/etc/systemd/system/下)軟鏈結,如下:

[root@cdh1 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

如果修改了配置檔案,但未進行 過載 的話, 每次啟動、重啟都會爆出警告,類似如下:

[root@cdh01 system]# systemctl status bigdata_test_sh

。。。warning: bigdata_test_sh.service changed on disk. run 『systemctl daemon-reload』 to reload units.

因此,每次修改完畢配置檔案,都需要使用該命令進行配置檔案過載操作:

使用systemd管理swoole服務

systemd 是 linux 系統中新一代的初始化系統 init 它主要的設計目標是克服 sysvinit 固有的缺點,提高系統的啟動速度。很多新的linux發行版已經使用systemd取代了init,作為初始守護程序。swoole的伺服器程式可以編寫一段service指令碼,交由systemd進...

Linux 下使用 systemd 設定開機啟動項

下面的教程將介紹如何將命令或程式配置為開機啟動時自動執行的服務。完成配置之後,就可以通過 linux 命令列來 啟動 停止 禁用 這個服務了。建立服務 在樹莓派上建立乙個 service 檔案。例如 myscript.service unit description my service after...

使用systemd來構建你的服務

systemd 服務是一種以 service 結尾的單元 unit 配置檔案,用於控制由systemd 控制或監視的程序。簡單說,用於後台以守護精靈 daemon 的形式執行程式。service檔案編寫簡單易用 可以自動維持程序存活 強大的功能,可以取代pm2 自動收集程序輸出的輸出 可以看到sys...