Wie kann ich überprüfen, ob eine Bildschirmsitzung ausgeführt wird?

Wie kann ich überprüfen, ob eine Bildschirmsitzung ausgeführt wird?

Gibt es eine Möglichkeit zu prüfen, ob in Bash eine Bildschirmsitzung ausgeführt wird?
Beispiel:

if [screen is running]
  then
    screen -r          #if session is running then resume the session
  else
    screen "command"   #else start a new session
fi

Antwort1

Nutzen Sie die Umgebungsvariable PPID(Parent ) und beginnen Sie mitPID

$ ps -fp$PPID
UID        PID  PPID  C STIME TTY          TIME CMD
w3       19305 19304  0 00:00 ?        00:00:00 SCREEN
+w3@aardvark:~(0)$ 

oder,

ps -fp$PPID | head -n 2 | tail -n 1 | egrep -q SCREEN
screen_is_running=$((1 - ${PIPESTATUS[-1]}))
# screen_is_running == 1 for yes, 0 for No, -1 for egrep error

Dies funktioniert natürlich nicht, wenn Sie gespawnt, ausgeführt, nohupiert oder etwas Ähnliches ausgeführt und Ihren $PPIDBildschirm nicht angezeigt bekommen haben.

Wenn dies der Fall ist, könnten Sie mit pgrep, pstree, etwas erstellen, das der Kette zurück egrepfolgen könnte (Stopp, wenn 1 ist).$PPID$PPID

Antwort2

Beim Lesen man screenerfahren Sie COMMAND LINE OPTIONS:

COMMAND-LINE OPTIONS
       Screen has the following command-line options:

    ...snip...

       -d -r   Reattach a session and if necessary detach it first.

       -d -R   Reattach a session and if necessary detach or even create it first.

       -d -RR  Reattach a session and if necessary detach or create it. Use the first session if more than one session is available.

       -D -r   Reattach a session. If necessary detach and logout remotely first.

       -D -R   Attach  here  and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first.  If it was not running create it and notify
               the user. This is the author's favorite.

       -D -RR  Attach here and now. Whatever that means, just do it.

            Note: It is always a good idea to check the status of your sessions by means of "screen -list".

Sicherlich würde eines davon auch ohne die Variable das gewünschte Ergebnis erzielen.

verwandte Informationen