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 screens and whether there is an active script running in them.



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.

  • Thank you. How can I kill the screen when I find out there is no processes running in it? – farjam Feb 25 '16 at 17:54
  • @farjam Edited. – Parthian Shot Feb 25 '16 at 18:15

You can prevent screen from sitting idle after a script finishes by terminating the shell when the script completes:

script.sh; exit

Or, if you want to have a chance to look at error messages, you could run

script.sh && exit

That way, screen will only close if script.sh completed successfully.

  • And when the OP finds a time machine, this will answer the question asked. – Parthian Shot Feb 25 '16 at 23:53
  • I appreciate this answer, however; it is not directly addressing the problem on hand, it is more of a solution to prevent having idle screens in future. – farjam Feb 26 '16 at 1:36
  • @ParthianShot are you assuming the OP will never use a computer again, not before he finds a time machine? – Dmitry Grigoryev Feb 26 '16 at 6:47
  • He asked for the time, and you told him to buy a watch. – Parthian Shot Feb 26 '16 at 7:08
  • @ParthianShot Yes, I did exactly that. You seem to imply it was the wrong thing to do, if so, feel free to downvote, flag it as "not an answer", or bring it up on Meta. – Dmitry Grigoryev Feb 26 '16 at 7:18

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.