新しい端末ウィンドウでスクリプトを実行する

新しい端末ウィンドウでスクリプトを実行する

現在のウィンドウを使用可能な状態に保ちながら、他の端末ウィンドウで親スクリプトとは別にスクリプトを実行したいと思います。

その理由は、ユーザーがディレクトリの変更を監視する監視スクリプトを実行できるようにしたいからです。

それは私の責任です。

function watchFunction ()
{
  ./watch.sh &
}    

ただし、これは現在のウィンドウの背景でのみ実行され続けます。

Linuxディストリビューションのため、genone-terminalまたは他のインストール可能なツールを使用またはインストールすることはできません。

bashスクリプトを始めたばかりだったので、どんなアドバイスでも役に立ちます!

ベストアンサー1

スクリプト出力のみを表示したい場合は、スクリプト出力をファイルにリダイレクトし、別のウィンドウでそのファイルを表示できます。

# Run the script and log all output to a file
./watch.sh &> /var/log/watch.log &

# Watch the file, possibly in another terminal window
tail -f /var/log/watch.log

私の経験では、この動作(ログファイルに書き込む)は非常に一般的です。別のターミナルウィンドウの作成を開始したコマンドラインアプリケーションを使用したことはありません。

つまり、コマンドラインで新しいターミナルウィンドウを開くには、ターミナルアプリケーションによって異なります。 AskUbuntu StackExchangeのサイトには、これに関する良い記事があります。

特に参照この回答。たとえば、Gnome 端末では、次のコマンドを使用できます。

gnome-terminal -x sh -c "./watch.sh; bash"

どの端末アプリケーションが使用されているかをプログラムで確認するには、次のAskUbuntu投稿を参照してください。

許容されるソリューションは、次の機能を定義します。

which_term(){
    term=$(perl -lpe 's/\0/ /g' \
           /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline)

    ## Enable extended globbing patterns
    shopt -s extglob
    case $term in
        ## If this terminal is a python or perl program,
        ## then the emulator's name is likely the second 
        ## part of it
        */python*|*/perl*    )
         term=$(basename "$(readlink -f $(echo "$term" | cut -d ' ' -f 2))")
         version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
         ;;
        ## The special case of gnome-terminal
        *gnome-terminal-server* )
          term="gnome-terminal"
        ;;
        ## For other cases, just take the 1st
        ## field of $term
        * )
          term=${term/% */}
        ;;
     esac
     version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
     echo "$term  $version"
}

おすすめ記事