同様のサービスが実行されている場合、Systemd は通知 PID を無視します。

同様のサービスが実行されている場合、Systemd は通知 PID を無視します。

パスを除くと、ほぼ同じ2つのサービスがあります。各サービスはtmuxでMinecraftサーバーを実行します。これは一般的なユニットファイルです:

[Unit]
Description=<description>
After=network.target

[Service]
Type=notify
NotifyAccess=all
ExecStart=<Path to start script>
ExecStop=<path to stop script>
WorkingDirectory=<path to dir>
Restart=on-failure
RestartSec=120
User=<user>

[Install]
WantedBy=multi-user.target

起動スクリプト:

#!/bin/bash

SESSION_NAME=<name>
SERVER_JAR=<jar path>
MEMORY=<ram>

tmux new-session -d -s ${SESSION_NAME} 'java -Xms'"${MEMORY}"' -Xmx'"${MEMORY}"' -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:InitiatingHeapOccupancyPercent=15 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -Dpaper.playerconnection.keepalive=120 -Dsession_name='"${SESSION_NAME}"' -jar '"${SERVER_JAR}"' nogui'

main_pid=$(pgrep -a java | awk '/session_name='"${SESSION_NAME}"'/ {print $1}')
echo ${main_pid}

systemd-notify --ready --pid=${main_pid}

スクリプトを停止します。

#!/bin/bash
  
SESSION_NAME=<name>

# 10 second warning
echo "10 Second warning"
tmux send-keys -t ${SESSION_NAME} 'say "Server shutting down in 10 seconds"' ENTER

sleep 10 # wait 10 seconds

# Stop server
echo "Stopping server..."
tmux send-keys -t ${SESSION_NAME} 'stop' ENTER

# Wait for screen to terminate
while tmux ls | grep -q ${SESSION_NAME}
do
        sleep 1
done
echo "Server stopped"

まったく同じ方法で設定されていますが、名前とパスが異なる2つのサービスがあります。最初のサーバーを起動すると正常に動作し、正しいPIDを受け取ります。ただし、2 番目のサービスを開始するとすぐに終了し、停止スクリプトが実行されます。 Systemdは通知メッセージを受信したにもかかわらず、2番目のプロセスのPIDを無視しているようです(systemd-notifyを介してステータスを設定できます)。問題は、1つのサービスがすでに実行されている場合にのみ発生します。どの順序で開始しても、2番目のサービスは常にこのように失敗します。

なぜこれが起こるのか、どのように解決するのか知りたいです。新しいサーバーを管理して設定する方が簡単で、新しいサーバーを作成するときにselinuxを使用する面倒を避けることができるので、起動/停止スクリプトを使い続けたいと思います。

私のシステムはRocky Linux 8.5で、systemd 250を実行しています。

ベストアンサー1

おすすめ記事