Systemd - 在ExecStopPost中检测服务是否退出而没有错误

问题描述 投票:6回答:1

我有一个应用程序,它完成后正常退出不应该重新启动。在此应用程序完成其业务之后,我想关闭实例(ec2)。我正在考虑使用带有选项的systemd单元文件来执行此操作

Restart=on-failure
ExecStopPost=/path/to/script.sh

应该在ExecStopPost上运行的脚本:

#!/usr/bin/env bash

# sleep 1; adding sleep didn't help

# this always comes out deactivating
service_status=$(systemctl is-failed app-importer) 

# could also do the other way round and check for failed
if [ $service_status = "inactive" ] 
then
  echo "Service exited normally: $service_status . Shutting down..."
  #shutdown -t 5
else
  echo "Service did not exit normally - $service_status"
fi
exit 0

问题是,当后停止运行时,我似乎无法检测服务是否正常结束,然后状态是deactivating,只有在我知道它是否进入failed状态之后。

linux systemd
1个回答
4
投票

你的问题是systemd认为服务是deactivating,直到ExecPostStop进程完成。睡觉并没有帮助,因为它只会等待更长时间。 ExecPostStop的想法是清理服务可能留下的任何东西,比如临时文件,UNIX套接字等。服务没有完成,并准备重新开始,直到清理完成。因此,如果你以这种方式看待它,那么systemd正在做什么是有意义的。

你应该做的是检查脚本中的qazxsw poi,qazxsw poi和/或qazxsw poi,它将告诉你服务是如何停止的。例:

$SERVICE_RESULT

当服务允许运行完成时:

$EXIT_CODE

当服务在完成之前停止时:

$EXIT_STATUS
© www.soinside.com 2019 - 2024. All rights reserved.