systemd 计时器需要在关机/重启时运行 ExecStop

systemd 计时器需要在关机/重启时运行 ExecStop

场景是我有一个 CentOS v7.0 系统,最多可以运行 4 个 JIRA 实例,这些实例分别是 Production、Staging、Development 和 BETA。

当我启动系统时,我希望所有四个服务实例以交错方式启动,间隔 100 秒(每个 JIRA 实例启动大约需要 80 秒)。我能够通过使用 systemd 计时器来解决交错启动问题(这肯定比我在 SysV inits 中使用的 shell 代码优雅得多)。每个服务都在自己的切片中运行,并通过切片控件设置适当的 QOS 级别。一切运行得非常好。

我遇到的问题是,当我发出停止/关闭/重新启动时,只有 jira_*.timer 实例被 systemd 关闭脚本调用,而 JIRA 实例没有被正确关闭。

如何在关闭/重新启动期间触发 jira_*.service 单元中的 ExecStop 操作?

PRD = 5sec delay
STG = 100sec delay
DEV = 200sec delay
EAP = 300sec delay

/usr/lib/systemd/system/jira_stg.service

[Unit]
Description=Atlassian JIRA Staging instance
Documentation=https://confluence.atlassian.com/display/JIRA/JIRA+Documentation
After=mysql.service nginx.service
Requires=mysql.service nginx.service
Before=shutdown.target reboot.target halt.target
[Service] 
Type=forking
ExecStart=/jira/stg/catalina.home/bin/startup.sh
ExecStop=/jira/stg/catalina.home/bin/shutdown.sh 60
TimeoutSec=300
User=ujirastg
Group=gjirastg
Slice=jira_stg.slice
CPUAccounting=true
CPUShares=600
MemoryAccounting=true
MemoryLimit=1200M
BlockIOAccounting=true
BlockIOWeight=200
[Install]
WantedBy=multi-user.target

/usr/lib/systemd/system/jira_stg.timer

[Unit]
Description=Atlassian JIRA Staging instance service startup after delay
[Timer]
# Time to wait after systemd starts before we start the service
OnStartupSec=100s
AccuracySec=5s
Unit=jira_stg.service
[Install]
WantedBy=multi-user.target

我只启用 jira_* .timer 单元,因为我发现如果我启用 jira_* .service 单元,计时器将被忽略,并且所有内容都会尝试立即启动。

systemctl enable jira_eap.timer
systemctl enable jira_dev.timer
systemctl enable jira_stg.timer
systemctl enable jira_prd.timer

来自journalctl,显示重启期间触发的计时器。

jira systemd[1]: Stopping Flexible branding.
jira systemd[1]: Stopped Flexible branding.
jira systemd[1]: Stopping Timers.
jira systemd[1]: Stopped target Timers.
jira systemd[1]: Stopping Atlassian JIRA Early Access Program instance service startup after delay.
jira systemd[1]: Stopped Atlassian JIRA Early Access Program instance service startup after delay.
jira systemd[1]: Stopping Atlassian JIRA Development instance service startup after delay.
jira systemd[1]: Stopped Atlassian JIRA Development instance service startup after delay.
jira systemd[1]: Stopping Atlassian JIRA Staging instance service startup after delay.
jira systemd[1]: Stopped Atlassian JIRA Staging instance service startup after delay.
jira systemd[1]: Stopping Daily Cleanup of Temporary Directories.
jira systemd[1]: Stopped Daily Cleanup of Temporary Directories.
jira systemd[1]: Stopping Atlassian JIRA Production instance service startup after delay.
jira systemd[1]: Stopped Atlassian JIRA Production instance service startup after delay.
jira systemd[1]: Stopping Sockets.
jira systemd[1]: Stopped target Sockets.

答案1

我发现 systemd 文档中提到的一个有点黑客的解决方案似乎运行良好。

http://www.freedesktop.org/software/systemd/man/systemd-halt.service.html

在执行实际系统halt/poweroff/reboot/kexec之前,systemd-shutdown将运行/usr/lib/systemd/system-shutdown/中的所有可执行文件,并向它们传递一个参数:“halt”、“poweroff”、“rebo​​ot” " 或 "kexec",具体取决于所选操作。该目录中的所有可执行文件都是并行执行的,并且在所有可执行文件完成之前不会继续执行操作。

/usr/lib/systemd/system-shutdown/jira_shutdown.sh

#!/bin/sh
case "$1" in
  halt|poweroff|reboot|kexec)
  # Shutdown any running JIRA instances
  for ENVIRONMENT in eap dev stg prd
  do
    STATUS=$(/usr/bin/systemctl is-active jira_${ENVIRONMENT}.service)
    if [ ${STATUS} == "active" ]; then
      /usr/bin/systemctl stop jira_${ENVIRONMENT}.service
    fi
  done
  ;;
  *)
  ;;
esac

相关内容