4

For a project I am developing I would like to detect if a screen is running as the current user, attached or detached. I would like an approach that uses the minimal number of processes and will be portable across distros/unix based OSes.

Option 1

ps -U $USER|grep screen|grep -v grep &>/dev/null && screen-yes

This appears quite untidy but it does do the job. the additional grep was needed for Darwin but not CentOS, is there a better way?

Option 2

screen -list|egrep '(There is a screen on|There are screens on)' &>/dev/null && screen-yes

This directly uses the screen command, but due to the relatively human style output that large regex is needed.

Is there a better way to do this?

2
  • If the reason you want to check is that you want to start a new session if none is running yet, but reconnect to an already-existing one it there is, note that you can run screen with some options to do just that... Nov 24, 2015 at 1:21
  • Yes, to achieve this: screen -D -R . I wanted to detect screen for this feature: github.com/benvaljean/bgrc/wiki/Post-login%20icons Nov 24, 2015 at 17:23

4 Answers 4

3

You should be able to use the return code from screen. It is true if there are screens, and false if there are none.

screen -list >/dev/null && screen-yes
3
  • 1
    Not in my testing: screen-list returns 1 if there are screens or no screens. What system are you running? Nov 23, 2015 at 20:40
  • That's unfortunate, as I'd expect gnu screen to work the same everywhere. I have screen-4.3.1 on fedora 21. Also ok on 4.01.0 raspberry pi debian.
    – meuh
    Nov 23, 2015 at 20:43
  • 1
    I believe this is the correct answer for any version of Screen from the 21st century. Just from a very cursory and very inexpert review of the source, versions prior to about 3.2 may have exhibited this behavior. But that would be a very old screen indeed—like 1991 old. Newer versions should exhibit the described behavior and exit with status 1 only if no existing sessions are found.
    – Kevin E
    Dec 20, 2023 at 2:01
2

My solution would be a variation of option 1:

ps -U $USER | grep [s]creen &>/dev/null && screen-yes

The square brackets around the first letter of the screen string use shell glob magic to avoid getting the grep command returned in the output. Same net effect, one less grep command.

1
  • That does work. I also found I could use pgrep in this way: pgrep -U $USER screen &>/dev/null && echo yes . I am not sure how portable pgrep is compared to ps. I can see pgrep on Debian 5 (old) though. Nov 23, 2015 at 20:35
1
if test ! -z "$STY" 
then    
    echo "I am running in GNU Screen"
fi

Is a quick solution which does not require external programs.

2
  • The $STY variable is present only if the shell is inside a screen. The requirement is to ascertain if there are any screens running for $USER on the host. Nov 9, 2018 at 8:58
  • Doesn't meet the requirements of the OP's question, but was exactly what I was looking for. This wasn't super obvious, even after scrutinizing the manual page. So thanks!
    – Kevin E
    Dec 20, 2023 at 1:01
0
pgrep -U $USER screen &>/dev/null && screen-yes

This does not require any additional processes and solves the problem with relatively long regex with option 1, using >1 process with option 2. I found pgrep on some fairly old systems so it should be portable.

1
  • This does not work on Ubuntu 16.04 (at least) due to kscreen_backend_launcher May 8, 2018 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .