Question: I have started a few processes in the past initiated via the `screen` command. Most of those scripts have already finished running, but looks like the screens are still sitting there idle. I can see them when I do a `ps aux | less` to see all processes. 

How can I see all the `screen`s and whether there is an active script running in them. 

Thanks

Answer: 
To see the current list of running screens:

```
screen -list

```

The first part of the screen's name is its PID. To see the tree of currently running processes spawned from that parent process, run:

```
pstree <PID>

```

or, for a more detailed output,

```
pstree -a <PID> | less

```

To reattach to a screen (and detach it if it's already attached elsewhere), run:

```
screen -rd <PID>

```

To kill a screen once you've established you no longer need it, you have a couple options. If you're currently attached to the screen, you can simply use `Ctrl` + `a`, `k`, which will kill the screen and all its windows.

Alternatively, if you're not attached to the session, you can use:

```
kill <PID>
screen -wipe <PID>

```

to kill it and remove it from the list of screens.
Using this pipe I can get a list of PIDs corresponding to inactive screens

```
screen -list | sed '1d;$d' | sed -r 's/^[^[0-9]]*([0-9]*).*$/\1/' | xargs -n1 pstree -p | sed -r 's/.*bash\(([0-9]+)\)$/\1/' | grep "^[0-9]*$"

```

For example:

```
> screen -list

There are screens on:
    8988.pts-0.ubuntu-1gb-sfo1-01   (07/09/2018 09:02:14 PM)    (Detached)
    8960.pts-0.ubuntu-1gb-sfo1-01   (07/09/2018 09:01:40 PM)    (Detached)
    8927.pts-0.ubuntu-1gb-sfo1-01   (07/09/2018 09:01:02 PM)    (Detached)
    8903.pts-0.ubuntu-1gb-sfo1-01   (07/09/2018 09:00:42 PM)    (Detached)
    8857.pts-0.ubuntu-1gb-sfo1-01   (07/09/2018 08:59:30 PM)    (Detached)
    8831.pts-0.ubuntu-1gb-sfo1-01   (07/09/2018 08:59:01 PM)    (Detached)
6 Sockets in /var/run/screen/S-user.

> screen -list | sed '1d;$d' | sed -r 's/^[^[0-9]]*([0-9]*).*$/\1/' | xargs -n1 pstree -p

screen(8988)───bash(8989)
screen(8960)───bash(8961)
screen(8927)───bash(8928)
screen(8903)───bash(8904)
screen(8857)───bash(8858)───python(8877)───{python}(8881)
screen(8831)───bash(8832)───python(8851)───{python}(8855)

```

The first 4 are inactive, so then it's a matter of picking out those PIDs, which can be done with `sed -r 's/.*bash\(([0-9]+)\)$/\1/' | grep "^[0-9]*$"`

What's nice about this is I can kill all the inactive screens with one line:

```
screen -list | sed '1d;$d' | sed -r 's/^[^[0-9]]*([0-9]*).*$/\1/' | xargs -n1 pstree -p | sed -r 's/.*bash\(([0-9]+)\)$/\1/' | grep "^[0-9]*$" | xargs -n1 kill

```