systemd:实现 Linux 服务 Crash 后自动重启systemd:

Systemd 允许你对服务进行配置,以便在服务崩溃时自动重启。

一个典型的单元文件是这样的:

[Unit]Description=Tailscale node agentAfter=network-online.targetWants=tailscale-weekly-update.timer
[Service]Type=oneshotExecStart=/usr/bin/tailscale update -yes
[Install]WantedBy=multi-user.target

在上面的例子中,如果守护进程崩溃或被杀死,systemd 不会去管它。

不过,你可以让 systemd 自动重启守护进程,以防它崩溃或意外被杀掉。为此,你可以在 [Service] 中添加 Restart 选项。典型的示例如下:

[Unit]Description=Lightweight KubernetesDocumentation=https://k3s.ioWants=network-online.targetAfter=network-online.target
StartLimitIntervalSec=600StartLimitBurst=5
[Install]WantedBy=multi-user.target
[Service]Type=notifyEnvironmentFile=-/etc/systemd/system/k3s.service.envKillMode=processDelegate=yesLimitNOFILE=1048576LimitNPROC=infinityLimitCORE=infinityTasksMax=infinityTimeoutStartSec=0Restart=alwaysRestartSec=5sExecStartPre=/bin/sh -xc '! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service'ExecStartPre=-/sbin/modprobe br_netfilterExecStartPre=-/sbin/modprobe overlayExecStart=/usr/local/bin/k3s \ server \

上述操作会对任何导致守护进程停止的情况做出反应…只要守护进程停止,systemd 就会在 5 秒内重启它。

Restart 有 2 个可选参数:

alwayson-failure: 即故障时重启. 涵盖了最广泛的故障情形,如信号不清和退出代码不清:

在本例中,[Unit] 部分还有 StartLimitIntervalSec 和 StartLimitBurst 指令。这可以防止故障服务每 5 秒钟重启一次。如果仍然失败,systemd 将停止尝试启动服务。

如果服务在 600 秒内 5 次尝试重启均未成功,则应进入失败状态,不再尝试重启。这样就能确保如果服务真的坏了,systemd 不会继续尝试重启它。应该人工上去处理了。

如果在守护进程被杀死后询问其状态,systemd 会显示正在activating (auto-restart)

Systemd OnFailure

重启一项服务固然很好,但在某个单元出现故障时采取特定行动就更好了。也许你使用的软件有一个已知的错误,要求在崩溃时删除缓存文件,也许你想启动一个脚本来收集日志和系统信息,以便诊断问题。Systemd 允许你指定在服务失败时运行的单元。

[Unit]Description=Lightweight KubernetesDocumentation=https://k3s.ioWants=network-online.targetAfter=network-online.target
StartLimitIntervalSec=600StartLimitBurst=5OnFailure=k3s-recovery.service
[Install]WantedBy=multi-user.target
[Service]Type=notifyEnvironmentFile=-/etc/systemd/system/k3s.service.envKillMode=processDelegate=yesLimitNOFILE=1048576LimitNPROC=infinityLimitCORE=infinityTasksMax=infinityTimeoutStartSec=0Restart=on-failureRestartSec=5sExecStartPre=/bin/sh -xc '! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service'ExecStartPre=-/sbin/modprobe br_netfilterExecStartPre=-/sbin/modprobe overlayExecStart=/usr/local/bin/k3s \ server \

此示例指定 OnFailure=k3s-recovery.service 来告诉 systemd,如果我的服务失败,它就应该启动 k3s-recovery 单元.

k3s-recovery 单元只是一个运行此脚本的一次性服务单元:

[Unit]Description=K3s recovery
[Service]Type=oneshotExecStart=/usr/local/sbin/k3s-recovery.sh

这个脚本可以做任何事情:执行一些手动变通方法让服务重新运行,向监控系统发出警报,或者压缩一些临时日志和应用程序状态以排除故障。示例如下:

#!/bin/bash
echo 'Attempting to recover!' > /tmp/recovery_infosystemctl stop k3s.service/usr/local/sbin/k3s-killall.shsystemctl start k3s.service

Systemd FailureAction reboot

还有一种可能, 重启治百病! 所以 systemd 内置了在单元故障时触发系统重启的功能。在本例中,当单元发生故障时,系统将优雅地重新启动:

[Unit]Description=Lightweight KubernetesDocumentation=https://k3s.ioWants=network-online.targetAfter=network-online.target
StartLimitIntervalSec=600StartLimitBurst=5FailureAction=reboot
[Install]WantedBy=multi-user.target
[Service]Type=notifyEnvironmentFile=-/etc/systemd/system/k3s.service.envKillMode=processDelegate=yesLimitNOFILE=1048576LimitNPROC=infinityLimitCORE=infinityTasksMax=infinityTimeoutStartSec=0Restart=on-failureRestartSec=5sExecStartPre=/bin/sh -xc '! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service'ExecStartPre=-/sbin/modprobe br_netfilterExecStartPre=-/sbin/modprobe overlayExecStart=/usr/local/bin/k3s \ server \

FailureAction 有多种有效值: nonerebootreboot-forcereboot-immediatepoweroffpoweroff-forcepoweroff-immediateexitexit-forcesoft-rebootsoft-reboot-forcekexeckexec-forcehalthalt-force 和 halt-immediate.

总结

本文介绍了服务异常时, 自动处理故障的一些方式。Systemd 包含强大的功能,可自动响应以保持服务运行。

文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/9525

(0)
郭靖的头像郭靖
上一篇 2023年8月14日 下午4:59
下一篇 2023年8月15日 下午4:50

相关推荐

  • Windows配置磁盘监控

    通过pushgateway的方式,主动推送监控数据给prometheus。 由于windows平台的诸多限制,导致推送比较艰难。有两种方法完成监控。 1.通过python等语言,做推送脚本。 2.曲线救国就是把监控数据scp发给Linux机器。再通过linux做二次加工推给pushgateway。 window server上配置环境 1.首先判断服务器磁盘…

    2022年11月25日
    1.1K00
  • ansible 用普通用户sudo 执行命令

    背景: linux 机器都禁止root用户远程直接登录,需要ansible切换用户来实现 配置 /etc/ansible/hosts [test] 192.168.55.65 ansible_ssh_user=vmuser ansible_ssh_pass=’112233′ ansible_become_pass=’778899′ 192.168.55.66…

    Linux系统 2023年2月6日
    1.4K00
  • k8s-重启Pod方法

    kubectl 没有 restart pod 这个命令,主要是由于在 k8s 中pod 的管理属于rs 等控制器,并不需要手动维护,但有时更新了yaml文件后,期望破都能够”重启”重新加载yaml文件,比如修改了configmap 的配置文件后,希望重启pod 加载配置,此时就需要 “重启” Pod。而”重启”…

    2023年10月9日
    1.3K00
  • Centos7上安装Zabbix6.0

    1、先安装依赖,mysql,nginx,php mysql安装,mysql版本要8.0及以上 Nginx安装 PHP 部署 启动后即可在浏览器通过ip访问到测试页,如果访问失败,检查ip80端口是否开放 2、zabbix安装 选择版本 看别人的资料说6.0在centos7系统只能通过source安装,所以到官网选择source安装,选择TLS长久维护版 安装…

    2023年5月31日
    1.1K00
  • Linux——手把手教你解决sudo指令无法使用的问题

    解决sudo指令无法使用的问题 1. 为什么不能使用 sudo指令能够使某一条指令拥有root权限,即以root权限去执行 例如 sudo ls -l 但是,如果是新创建的普通账户,一般来说一开始是不能执行sudo命令的: 根据提示,LQF这一用户并不在sudoers这个文件中,因此没有使用sudo的权利suduers文件位于路径/etc/sudoerssu…

    2024年5月31日
    1.3K00

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

在线咨询: QQ交谈

邮件:712342017@qq.com

工作时间:周一至周五,8:30-17:30,节假日休息

关注微信