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

相关推荐

  • VMware虚拟机安装Ubuntu24.04教程

    (一)Ubuntu镜像文件下载(选择Ubuntu桌面版)1、Ubuntu官方网站(1)Ubuntu官网:https://ubuntu.com(2)Ubuntu官网中文站:https://cn.ubuntu.com(3)Ubuntu24.04桌面端官方下载:https://ubuntu.com/blog/ubuntu-desktop-24-04-noble-n…

    2024年5月31日
    1.2K00
  • Ubuntu修改静态IP、网关和DNS的方法总结

    /etc/netplan (use) Ubuntu 18.04开始可以使用netplan配置网络,其也是默认安装的。配置文件位于/etc/netplan/xxx.yaml中,netplan默认是使用NetworkManager来配置网卡信息的。 使用 netplan apply 网络配置 Ubuntu /etc/network/interfaces 修改静态…

    2023年11月27日
    72500
  • DNS服务器搭建与正反解析配置

    DNS服务介绍 DNS(Domain Name System–域名系统),是因特网的一项服务。它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网。是一个应用层的协议DNS使用TCP和UDP端口53。 DNS是一个分布式数据库,命名系统采用层次的逻辑结构,如同一颗倒置的树,这个逻辑的树形结构称为域名空间,由于DNS划分了域名空间,所以…

    2024年5月15日
    46600
  • centos7 shell 支持中文显示

    其实很简单,让 shell 支持 utf8 即可。先使用locale命令看一下设置情况: LC_ALL没有设置,需要赋值en_US.UTF-8,很简单: 最后,不要忘了source一下bash_profile,让其生效。

    2023年3月14日
    86700
  • CentOS 修改 SSH无操作自动断开时长及连接超时

    CentOS 修改 SSH无操作自动断开时长 当 SSH 连接到 CentOS 服务器时, 如果一段时间不操作, SSH 会自动断开。 这时, 可修改配置增加连接时长。 一、修改配置 在配置中找到 修改为 ClientAliveInterval 指定了服务器端向客户端发送消息的间隔,默认 0 不发送消息。ClientAliveInterval 30…

    2023年2月20日
    1.6K00

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信