为什么主进程终止后,systemd 不重新启动具有 Restart=always 的简单服务?

为什么主进程终止后,systemd 不重新启动具有 Restart=always 的简单服务?

我创建了一个简单的 systemdtest.service服务Restart=always

[Unit]
Description=test

[Service]
Type=simple
Restart=always
RemainAfterExit=no
ExecStart=/home/core/test.sh

test.sh

#!/usr/bin/env bash
set -e

sleep 1h

systemctl start test.service和之后systemctl status test.servce显示test.sh主流程和sleep子流程。

● test.service - test
     Loaded: loaded (/etc/systemd/system/test.service; linked; preset: disabled)
     Active: active (running) since Wed 2023-09-06 12:53:51 CST; 2s ago
   Main PID: 2032464 (bash)
      Tasks: 2 (limit: 127633)
     Memory: 736.0K
        CPU: 4ms
     CGroup: /system.slice/test.service
             ├─2032464 bash /home/core/test.sh
             └─2032465 sleep 1h

然后test.sh通过杀死sudo kill 2032464并再次执行systemctl status test.servce,主进程被杀死而sleep子进程存在。

● test.service - test
     Loaded: loaded (/etc/systemd/system/test.service; linked; preset: disabled)
     Active: active (running) since Wed 2023-09-06 12:53:51 CST; 9s ago
    Process: 2032464 ExecStart=/home/core/test.sh (code=killed, signal=TERM)
   Main PID: 2032464 (code=killed, signal=TERM)
      Tasks: 1 (limit: 127633)
     Memory: 204.0K
        CPU: 4ms
     CGroup: /system.slice/test.service
             └─2032465 sleep 1h

test.service为什么 systemd在主进程终止后不重新启动?

操作系统环境

NAME="Flatcar Container Linux by Kinvolk"
ID=flatcar
ID_LIKE=coreos
VERSION=3510.2.6
VERSION_ID=3510.2.6
BUILD_ID=2023-08-07-1638
SYSEXT_LEVEL=1.0
PRETTY_NAME="Flatcar Container Linux by Kinvolk 3510.2.6 (Oklo)"
ANSI_COLOR="38;5;75"
HOME_URL="https://flatcar.org/"
BUG_REPORT_URL="https://issues.flatcar.org"
FLATCAR_BOARD="amd64-usr"
CPE_NAME="cpe:2.3:o:flatcar-linux:flatcar_linux:3510.2.6:*:*:*:*:*:*:*"

Systemd 版本

systemd 252 (252)
+PAM +AUDIT +SELINUX -APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL -ACL +BLKID +CURL -ELFUTILS -FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY -P11KIT -QRENCODE -TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD -BPF_FRAMEWORK -XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified

答案1

新版 flatcar linux 容器版本 (3510.2.7) 已修复此问题

在稳定版本上升级到 systemd 252.11 后,Containerd 无法正常重启

相关内容