systemdタイマーは、シャットダウン/再起動時にExecStopを実行する必要があります。

systemdタイマーは、シャットダウン/再起動時にExecStopを実行する必要があります。

シナリオは、本番、ステージング、開発、ベータなど、最大4つのJIRAインスタンスを実行できるCentOS v7.0システムがあることです。

システムを起動するとき、4つのサービスインスタンスがすべて100秒間隔で視差を置いて開始されることを望みます(各JIRAインスタンスは起動に約80秒かかります)。私はsystemdタイマーを使用してパララックスブートの問題を解決できました(確かにSysV初期化で使用したシェルコードよりはるかにエレガントです)。各サービスは独自のスライスで実行され、スライス制御によって適切な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_* .serviceユニットを有効にすると、タイマーが無視され、すべてがすぐに起動しようとしていることがわかったので、jira_* .timerユニットのみを有効にしました。

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

実際のシステム quit/poweroff/reboot/kexec を実行する前に、systemd-shutdown は /usr/lib/systemd/system-shutdown/ にあるすべての実行可能ファイルを実行し、「halt」、「poweroff」、「reboot」という引数を渡します。または、「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

おすすめ記事