스크린 세션이 실행 중인지 확인하는 방법은 무엇입니까?

스크린 세션이 실행 중인지 확인하는 방법은 무엇입니까?

Bash에서 스크린 세션이 실행 중인지 확인할 수 있는 방법이 있습니까?
예를 들어:

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

답변1

PPID(부모 PID) 환경 변수를 활용 하고 다음으로 시작하십시오.

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

또는,

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

물론, 생성, 실행, nohup'd 등을 수행하고 $PPID화면을 표시하지 않은 경우에는 작동하지 않습니다.

그렇다면 체인을 뒤로 따라갈 수 있는 pgrep, pstree, 을 사용하여 무언가를 만들 수 있습니다 (1일 때 중지 ).egrep$PPID$PPID

답변2

읽으면 man screen다음을 찾을 수 있습니다 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".

확실히 이들 중 하나는 변수 없이 원하는 작업을 수행할 것입니다.

관련 정보