Skip to content

Instantly share code, notes, and snippets.

@r9y9
Created November 1, 2016 11:02
Show Gist options
  • Select an option

  • Save r9y9/01f781b390e0b5e285637f4080e75bac to your computer and use it in GitHub Desktop.

Select an option

Save r9y9/01f781b390e0b5e285637f4080e75bac to your computer and use it in GitHub Desktop.
#!/bin/bash
# ulbw 監視スクリプト
set -e
app_path="/Users/Shared/wlbw/wlbw_ir.app"
tlb_cmd_stop=$(cat << EOS
ps ax | grep $app_path | grep -v grep | awk {'print \"kill\", \$1'} | sh
EOS)
### 以下の変数は、必要であればシェルの引数から変更可能にする ###
# アプリの応答を待つ時間
timeout_sec_for_activate=10
# activateエラー検知時に、アプリをrestartするかどうか
enable_force_restart_when_activate_failed=1
# アプリ起動前に、一度アプリを強制終了する
enable_force_stop=1
# 強制終了後の待ち時間(秒)
delay_time=10
###############################################################################
# アプリ正常起動確認失敗時の処理
###############################################################################
if [ ${enable_force_restart_when_activate_failed} == 1 ]; then
on_activate_error=$(cat << EOS
tell application "System Events"
log "$tlb_cmd_stop"
do shell script "${tlb_cmd_stop}"
delay "$delay_time"
end tell
set posixAppFullPathInActivateFailed to "${app_path}"
set appPathInActivateFailed to POSIX file posixAppFullPathInActivateFailed
tell application "Finder" to open appPathInActivateFailed
EOS)
else
on_activate_error='log "do nothing on error"'
fi
###############################################################################
# アプリケーション起動前の処理
###############################################################################
if [ ${enable_force_stop} == 1 ]; then
before_launch_app=$(cat << EOS
tell application "System Events"
log "$tlb_cmd_stop"
do shell script "${tlb_cmd_stop}"
log "delay ${delay_time}"
delay "$delay_time"
end tell
EOS)
else
before_launch_app='log "force stop disabled"'
fi
###############################################################################
# メインの apple script
###############################################################################
osascript << EOF
set posixAppFullPath to "${app_path}"
if application posixAppFullPath is running then
log posixAppFullPath & "は running です"
try
with timeout of ${timeout_sec_for_activate} seconds
log "Running!"
end timeout
on error errMessage number errorNumber
display notification "An error occurred: " & errorNumber & " " & errMessage & "
Restarting ulbw..." as text with title "ulbw monitor"
log "ERROR: " & errorNumber &":" & errMessage
${on_activate_error}
end try
else
log posixAppFullPath & "は not running状態です。起動します"
${before_launch_app}
set appPath to POSIX file posixAppFullPath
tell application "Finder" to open appPath
end if
log "Finish"
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment