List processes being run inside a screen (Linux) session

5

1

I have several screen sessions, each one of them is running one java application.

For example, I have these 3 screen sessions :

    7361.ARM48      (Detached)
    7397.ROP90      (Detached)
    7314.ARM22      (Detached)

And this is the output of ps -A

7314 ?        00:00:00 screen
7329 pts/1    00:00:16 java
7361 ?        00:00:00 screen
7375 pts/2    00:00:02 java
7397 ?        00:00:00 screen
7411 pts/3    00:00:02 java

Is there any way to reliably detect whether each screen session is running their java application, or not?

I have looked around the screen Linux manual but couldn't find any screen commands that would show the child processes of a screen session (http://linux.die.net/man/1/screen)

Albert

Posted 2013-04-14T09:41:30.813

Reputation: 51

Have you tried to run htopand then press <kbd>F5</kbd> for tree view? – None – 2013-04-14T11:03:11.143

Thanks for the tip, it works fine indeed if I want to check that manually, but I need something more parsable as this is intended for a PHP script (htop output is too complicated to parse). I was hoping that screen had a command that would show the child proces(es) being run inside the session. – Albert – 2013-04-14T12:01:34.110

You can list the children of the current shell with ps. screen -S java1 -X stuff ps"$(printf '%b' '\015')" – Ярослав Рахматуллин – 2013-04-14T12:40:05.193

Answers

3

This sounds like a case for

ps fx

and it also works for several windows within one screen session

mjhoffmann

Posted 2013-04-14T09:41:30.813

Reputation: 31

Can you expand your answer a little to elaborate? – fixer1234 – 2015-04-20T23:24:09.110

man ps: f ASCII-art process hierarchy (forest); x Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the a option. – Jay – 2017-01-17T17:51:55.823

2

There are several options, the output of ps aux can be sorted on the name of the terminal and the pid. That will give you a list of every child process of every screen, but will not clearly associate (pts/N) each screen with the processes inside of it:

e.g.

$ ps aux | sort -k7.2r  | 
                grep -Ei 'pts|java|screen' | 
                awk '{ printf "%10s %6s %6s %6s %s\n", $1, $2, $7, $9, $11}'
  jaroslav  30700   tty3  Apr07 screen
      root   4933      ?  Mar16 SCREEN
  jaroslav   5024      ?  Mar12 SCREEN
      root   3811      ?  Apr07 sshd:
  jaroslav   3716      ?  Apr07 SCREEN
  jaroslav  11996      ?  Apr05 SCREEN
  jaroslav  11139      ?  Apr05 sshd:
      root   4934  pts/8  Mar16 -/bin/bash
      root   3819  pts/6  Apr07 -bash
  jaroslav   3718  pts/5  Apr07 -/bin/bash
  jaroslav   3810  pts/5  Apr07 ssh
  jaroslav  11998  pts/2  Apr05 -/bin/bash
  jaroslav  11140  pts/1  Apr05 -bash
  jaroslav  12594  pts/1  13:52 sort
  jaroslav  12595  pts/1  13:52 grep
  jaroslav  12596  pts/1  13:52 awk
  jaroslav  12593  pts/1  13:52 ps
  jaroslav   5041  pts/0  Mar12 /bin/bash
  jaroslav   5076  pts/0  Mar12 /usr/lib/jvm//sun-jdk-1.6/bin/

Alternatively, you could run pstree on every screen to list their children. This is probably what you are looking for.

$ echo $(pidof  screen;pidof  SCREEN)| tr ' ' \\n  |
       xargs -L1 pstree -lanu

creen,jaroslav -dR serv
screen,jaroslav -dR java3
  `-bash
      `-java -cp /home/jaroslav/src/java/ TestUlimit
          `-11*[{java}]
screen,jaroslav -dR java2
  `-bash
      `-java -cp /home/jaroslav/src/java/ TestUlimit
          `-11*[{java}]
screen,jaroslav -dR java1
  `-bash
      `-java -cp /home/jaroslav/src/java/ TestUlimit
          `-11*[{java}]
screen,jaroslav -dR serv
  `-bash
screen,jaroslav -dmS nailgun /home/jaroslav/bin/nailgun
  `-nailgun /home/jaroslav/bin/nailgun
      `-java -server -jar /usr/share/nailgun/lib/nailgun.jar localhost:64781
          `-16*[{java}]
screen -dR emr
  `-bash
screen,jaroslav -dR gdb
  `-bash
      `-ssh l00

Or, find the parents of each java process.

$ for i in `pidof  java`; do pstree -sp $i ;done | grep -v '^ '
init(1)---screen(13097)---bash(13099)---java(13943)-+-{java}(13947)
init(1)---screen(13004)---bash(13006)---java(13927)-+-{java}(13931)
init(1)---screen(12911)---bash(12913)---java(13911)-+-{java}(13915)
init(1)---screen(5024)---nailgun(5041)---java(5076)-+-{java}(5095)

Ярослав Рахматуллин

Posted 2013-04-14T09:41:30.813

Reputation: 9 076

echo $(pidof screen;pidof SCREEN)| tr ' ' \\n | xargs -L1 pstree -lanu This did exactly what I needed. Thanks! – Jay – 2017-01-17T17:48:53.217