AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1061283
Accepted
Aris Koutsoukis
Aris Koutsoukis
Asked: 2021-04-23 23:00:15 +0800 CST2021-04-23 23:00:15 +0800 CST 2021-04-23 23:00:15 +0800 CST

ExecStart 中的脚本退出时执行 ExecStop

  • 772

我对 systemd 服务的行为有点困惑。我有上述系统服务。

[Unit]
After=libvirtd.service

[Service]
Type=simple
Environment=VM_XML=xxxxxxx
ExecStartPre=/usr/bin/bash /usr/local/lib/common/createQcowImage.sh ${VM_XML}
ExecStart=/usr/bin/bash /usr/local/lib/common/createVM.sh ${VM_XML}
ExecStop=/usr/bin/bash /usr/local/lib/common/destroyVM.sh ${VM_XML}
Restart=always

[Install]
WantedBy=multi-user.target

当这个单元在 createVM.sh 脚本中启动时,它会创建一个 VM 并监控它的状态。如果 VM 的 PID 不再存在,则脚本以返回码 1 退出。我注意到,当发生这种情况时,ExecStop 被执行(我正在管理 /var/log/messages,当我手动销毁 VM 时)使用 virsh destroy 我在从 ExecStop 执行的脚本中播下用于调试的回声,以打印在 /var/log/messages 中)。这是 systemd 的默认行为吗?在单元退出时执行 ExecStop(我也尝试使用代码 0 退出,这是相同的行为)。这不是我真正想做的,因为在我销毁 VM 后,ExecStop 会尝试销毁导致 systemd 单元故障的同一 VM。有没有办法避免这种情况?

linux kvm-virtualization systemd
  • 2 2 个回答
  • 2088 Views

2 个回答

  • Voted
  1. Best Answer
    user1182474
    2021-05-16T14:03:17+08:002021-05-16T14:03:17+08:00

    如果您想创建一个按预期运行的 systemd 服务配置,您需要很好地了解所有选项的含义。

    我建议您开始阅读man systemd.service有关Type=, ExecStart=, ExecStop=. 也许Type=exec,或者Type=oneshot更适合您的脚本?

    您描述的行为在我看来确实符合预期。

    要处理您的 VM 已被其用户停止的情况,您需要测试它是否仍在运行,如果没有则避免破坏它。也不要忘记 ExecStop 必须等待机器停止,而不仅仅是要求它停止。

    我附上了我用来启动 VirtualBox VM 的用户服务,也许你可以在那里找到一些灵感。

    [Unit]
    Description=Virtual Machine as a headless service.
    After=network.target virtualbox.target
    Before=runlevel2.target shutdown.target
    
    [Service]
    Type=simple
    Restart=no
    Environment=VMNAME=my-vbox-vm
    Environment=TIMEOUT=60
    #ExecStartPre=VBoxManage snapshot $VMNAME take "before service start"
    ExecStart=VBoxHeadless --startvm $VMNAME
    # Sleep might be needed if there is some ExecStartPost later, to make sure that the VM was really powered up
    #ExecStartPost=sleep 1
    # sometimes it happens that the machine is paused from some reason
    #ExecStartPost=bash -c "VBoxManage showvminfo $VMNAME | grep -E ^State:\\\\s+paused && VBoxManage controlvm $VMNAME resume || exit 0"
    # If the VM uses the VirtualBox hdd encryption, so a line like below is needed to unlock the drive
    #ExecStartPost=VBoxManage controlvm $VMNAME addencpassword $VMNAME "/home/user/VirtualBox VMs/my-vbxo-vm/my-vbox-vm.vmdk.pass"
    # The 'savestate' is the only reliable way to turn off the machine.
    # Optionally sending acpishutdown, can be considered, but better that needs to use the savestate as a fallback anyway,
    # because the acpishutdown can be from various reasons ignored by the VM or delayed significantly.
    #
    # Alternatives for ExecStop
    # - save state
    #   (should be reliable but not really turning off the VM;
    #    exit 0 necessary to handle the situation that VM was turned off already,
    #    from inside)
    #   ExecStop=bash -c "VBoxManage controlvm $VMNAME savestate || exit 0"
    # - stop vm using acpi, with active waiting
    #   (not reliable - if acpi signal ignored by VM, or shutdown takes too long,
    #    systemd will just kill it after some timeout, (normally 90s);
    #    note that the active waiting is necessary,
    #    otherwise systemd might kill the process before shutdown is finished)
    #   ExecStop=bash -c "VBoxManage controlvm $VMNAME acpipowerbutton && while (VBoxManage list runningvms | grep $VMNAME); do sleep 1; done"
    # - stop vm using acpi or, after a timeout, with save state
    #   (timeout let's say max 60s, to keep 30s for possible savestate;
    #    exit 0 necessary in order not to have the service in error state when stopped already by acpi, or maybe even by user from inside of VM)
    ExecStop=bash -c "VBoxManage controlvm $VMNAME acpipowerbutton && (let timeout=$TIMEOUT; while VBoxManage list runningvms | grep --silent $VMNAME && test $timeout -gt 0; do sleep 1; let timeout=$timeout-1; done) && VBoxManage list runningvms | grep --silent $VMNAME && VBoxManage controlvm $VMNAME savestate || exit 0"
    
    [Install]
    WantedBy=default.target
    
    • 0
  2. Aris Koutsoukis
    2021-05-18T00:30:55+08:002021-05-18T00:30:55+08:00

    这是 systemd 的默认行为

    当使用ExecStart配置的进程是服务的主进程时,使用type=simple(默认设置)。此类单元将等到 ExecStart 指定的进程返回,然后通过运行 ExecStop 指定的进程来停用

    我设法通过在从 ExecStop 执行的脚本中添加一个小检查来解决我的问题,以便仅在 VM 存在时才销毁 VM。

    DOMAIN="$(xmllint --xpath '//domain/name/text()' $1)"
    tmp=$(/usr/bin/virsh list --all | grep "$DOMAIN" | awk '{ print $3}')
    if ([ "$tmp" != "" ]);then
        echo "Destroying $DOMAIN"
        /usr/bin/virsh destroy $(xmllint --xpath '//domain/name/text()' $1)
    fi
    
    • 0

相关问题

  • 多操作系统环境的首选电子邮件客户端

  • 你最喜欢的 Linux 发行版是什么?[关闭]

  • 更改 PHP 的默认配置设置?

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve