GUIの「ターミナルで実行」でbashスクリプトが起動されたかどうかを確認するには?

GUIの「ターミナルで実行」でbashスクリプトが起動されたかどうかを確認するには?

bashスクリプトを実行します。ターミナルで実行し、「ターミナルで実行」を使用してGUIで実行し、対話式に実行してから終了したいと思いますbashここで説明されているように出力を確認してください。:に追加してくださいread -rn1

質問X
私は最後にbashと同じ端末を持ち、exitスクリプトが端末とGUIで実行されている場合は1つで閉じることができる「きれいな」ソリューションが欲しいです。を追加できますが、端末で実行している場合は、端末を閉じるのにbash -i2時間かかります。結果は同じです。exitexec bash -i

「ターミナルで実行」を介してGUIで実行されていることを確認する方法はスクリプトにありますか?

コメントごとに1つ追加:

ps aux | grep aaaa # while script started from GUI was running
mint       53293  0.1  0.0  11216  3356 pts/3    Ss+  21:58   0:00 /bin/bash /home/mint/aaaaa.sh

Ss+代わりに端末で実行するのと違いを確認してくださいS+

ベストアンサー1

あなたのbashスクリプト名が指定されたとします。/path/to/mybashscript.sh

ps照会に使用されますmybashscript.sh$0スクリプト内で実行されている場合)。state/stat特定の状態を識別する列が含まれます。

ps --sort +pid -eo pid,stat,command | grep "$0" | head -1 | awk '{print $2}' | grep "s"

または、行をフィルタリングする他の方法は次のとおりですgrep

ps -eo pid,stat,command | grep "$0" | grep -v grep | awk '{print $2}' | grep "s"

あなたのコメントによると、違いは以下を追加したためですss is a session leaderGUI方式の場合、Ubuntuにはファイルマネージャでスクリプトを起動して確認する方法はありません。

man ps状態コードは次のとおりです。

PROCESS STATE CODES
   Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display
   to describe the state of a process:

           D    uninterruptible sleep (usually IO)
           I    Idle kernel thread
           R    running or runnable (on run queue)
           S    interruptible sleep (waiting for an event to complete)
           T    stopped by job control signal
           t    stopped by debugger during the tracing
           W    paging (not valid since the 2.6.xx kernel)
           X    dead (should never be seen)
           Z    defunct ("zombie") process, terminated but not reaped by its parent

   For BSD formats and when the stat keyword is used, additional characters may be displayed:

           <    high-priority (not nice to other users)
           N    low-priority (nice to other users)
           L    has pages locked into memory (for real-time and custom IO)
           s    is a session leader
           l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
           +    is in the foreground process group

おすすめ記事