スクリプト内の他のプログラムに応答して実行中のプログラムを停止する最良の方法は何ですか?

スクリプト内の他のプログラムに応答して実行中のプログラムを停止する最良の方法は何ですか?

私のユースケースは、ファイルシステムをマウントする別のスクリプトを使用するyadプログラムです。私が現在やっていることとやや不満足なことは、複雑なkill and waitを介してyadで使用できる戻り値を「シミュレートする」ことです。

(マウントスクリプトはコードスニペットの上にあり、関連していません。終了または再マウント時に「FD」(SEL_FDではない)に書き込みを送信します。以下のコードは、GAME_SELとREMOUNTEDに従ってループを使用または使用しません。)

#stdout to FD (to get the selection on exit and still be a background proc)
GAME_SEL=$(mktemp -u -p "${XDG_RUNTIME_DIR}" "${0##*/}.XXXXXXXXXX")
mkfifo "$GAME_SEL"
exec {SEL_FD}<> "$GAME_SEL"

yad \
--width=600 \
--height=660 \
--center \
--window-icon "applications-games" \
--title "Dosbox Launcher with games overlay" \
--text=" Click to play"  \
--list \
--noheaders \
--search-column=2 \
--regex-search \
--column="Path:HD" \
--column="Game Config:TEXT" \
--button=gtk-close:1 \
--button=gtk-ok:0 \
"${CONF_LIST[@]}" \
 2>/dev/null 1>& "$SEL_FD" &
yad_pid=$!

#wait for copy-on-write-drive notification (quit or mount doesn't matter)
#and kill the dialog using yad signal for graceful failure termination
( read -u "$FD"; kill -USR2 $yad_pid ) &
pidof_killer=$!

#wait for yad termination and read its stdout on a timeout. If triggers timeout, it's empty
wait $yad_pid 2>/dev/null
read -t 0.1 -u "$SEL_FD" GAME
exec {SEL_FD}>&- #close FD

#kill the killer if still active
kill $pidof_killer 2>/dev/null
wait $pidof_killer 2>/dev/null #supresses 'Terminated' output too
#if the killer was already dead (it caught a notification), this is 0, otherwise != 0
REMOUNTED=$?

私が理解しているように、マウントスクリプトFD通知に応答してyadプロセスを終了できるように、バックグラウンドにyadプロセスを配置する必要があるため、この複雑さが必要です。これにより、yadの戻り値(stdoutの代わりに)がSEL_FDになります。キャプチャ)は、デフォルトのスクリプトプロセスではキャプチャできません。このロジックを実装するより良い方法はありますか?とても複雑だと思います。

ベストアンサー1

おすすめ記事