LoginSignup
0
1

アプリを再起動する shell script を書いた

Posted at

0. はじめに

再起動することが多いアプリの扱いに困っていたので shell script でコマンドを作った。

プログラムは Github にアップロードしました。

  • アプリを再起動する公式コマンドはほとんどの場合用意されていません。BetterTouchTool でしか見たことありません。
  • BetterTouchTool のアプリローカルなコマンドは、アプリ終了後にプロセス自体がおそらく死んでしまうので、機能しません。たまに上手く行っていた時期はあります。
  • Firefox は終了がゴリ重なので普通の終了ルートだと時間が足りません。

1. どうして再起動コマンドがいるの?

メモリ解放とかキャッシュクリアのためです(キャッシュは設定による)。

CPU 暴走にも対処したいですが、今回は欲張らずに Terminal でキーボードが打てる状況なら良いことにしました。

2. 環境

スペックがこんな感じ(^_^;)

プロセッサ:2.3 GHz クアッドコアIntel Core i5
グラフィックス:Intel Iris Plus Graphics 655 1536 MB
メモリ:8 GB 2133 MHz LPDDR3

なのでうっかりしていると頻繁にメモリ不足になります。OS は Ventura です。

よく分からないタイミングで DeepL が異常にCPUを占有するのも結構きついです。Firefox は言うまでもないです。

3. コード

とりあえず ブラウザとエディタ と DeepL について用意してみました。プロセス名を特定するのが障害なだけなので、そこをクリアすればどんなアプリでも行ける。

VSCode は上手く行かなかったので osascript を使ってひとつだけ分離した(。ŏ﹏ŏ) 起動済みかの確認ができない以外は問題ない。

restart_app.sh

#!/bin/zsh
# https://chat.openai.com/c/cedbd932-3df7-4c9e-96b2-1840b9356516

# Specify the name or command of the application to restart
#NAME_APP="your_application"

# Read user input for the application name
read -p "Enter the name or abbreviation of the application: " _input
interval="4";

# echo "$_input"
# Check if the input is ready and set the NAME_APP and PROCESS accordingly
if [[ "$_input" == "brave" || "$_input" == "bb" ]]; then
	NAME_APP="Brave Browser";
    PROCESS="Brave Browser";
elif [[ "$_input" == "firefox" || "$_input" == "ff" ]]; then
    NAME_APP="Firefox";
    PROCESS="Firefox";
    # interval="5";
elif [[ "$_input" == "DeepL" || "$_input" == "ll" ]]; then
    NAME_APP="DeepL";
    PROCESS="DeepL";
    # interval="5";
elif [[ "$_input" == "code" || "$_input" == "vs" ]]; then
    # NAME_APP="Visual Studio Code";
    # PROCESS="code"# not detected
    sh restart_vscode.sh
    exit 0
else
    echo "Unsupported application: $_input"
    exit 1
fi

# Set the path of the app
APP_PATH="/Applications/$NAME_APP.app"
echo "We open or reopen $NAME_APP placed on $APP_PATH\n"

# Function to check if the application is running
is_app_running() {
    pgrep -x -i "$PROCESS" > /dev/null
}

# Function to restart the application
restart_app() {
    echo "Restarting $NAME_APP..."
    pkill -x -i "$PROCESS"
    sleep "$interval"
    open "$APP_PATH"
}

# Check if the application is running
if is_app_running; then
    # If the application is running, restart it
    restart_app
    echo "$NAME_APP restarted successfully."
else
    # If the application is not running, start it
    echo "$NAME_APP is not running. Starting it..."
    open "$APP_PATH" &
    if is_app_running; then
        echo "$NAME_APP started successfully."
    else
        echo "Failed to restart $NAME_APP."
    fi
fi
restart_vscode.sh
NAME_APP="Visual Studio Code"
APP_PATH="/Applications/$NAME_APP.app"
interval="5";

echo "Restarting $NAME_APP..."

osascript -e 'quit app "Visual Studio Code"'
sleep "$interval"
open "$APP_PATH"

n. おわりに

アプリを再起動する shell script を書きました。

コード:Github

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1