使用systemd 脚本设置服务keepalive

一. 适用场景

  • 长时间运行, 且不能停止的任务,如 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

[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-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: @app.long.name@
  cloudResetPwdUpdateAgent.service    loaded    active running LSB: @app.long.name@
  crond.service                       loaded    active running Command Scheduler
  dbus.service                        loaded    active running D-Bus System Message Bus
  getty@tty1.service                  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/下)软链接,如下:

[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

4.6 xxx.service修改后重新加载

如果修改了配置文件,但未进行 重载 的话, 每次启动、重启都会爆出警告,类似如下:

[root@cdh01 system]# systemctl status bigdata_test_sh
。。。
Warning: bigdata_test_sh.service changed on disk. Run ‘systemctl daemon-reload’ to reload units.

因此,每次修改完毕配置文件,都需要使用该命令进行配置文件重载操作:
[root@cdh1 system]# systemctl daemon-reload

五. init 与 systemd的命令区别

在这里插入图片描述

参考列表:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值