How to remove many detached screen sessions in Unix?

19,763

Solution 1

List show similar to below output

rajshah@rainbow:~$ screen -ls

There are screens on:
        8105.pts-152.rainbow    (Detached)
        5587.work     (Attached)
        20462.rajshah       (Attached) 3 Sockets in /var/run/screen/S-rajshah.

As screen sessions are stored in /var/run/screen/S-/

To remove any session,

rm -rf /var/run/screen/S-rajshah/8105.pts-152.rainbow

rajshah@rainbow:~$ screen -ls

There are screens on:
        5587.work     (Attached)
        20462.rajshah       (Attached) 3 Sockets in /var/run/screen/S-rajshah.

Solution 2

I know its old question but Here is what i did

Named sessions : when i open screen to have meaningful name id for some stuffs im doing related to superuser.com 
# screen -S superuser.com
.. < Ctrl + a + d > ..
# screen -ls
    21668.superuser.com (Detached)  
    21664.otography.com (Detached)
    17386.wimbledon (Detached)
    17200.unsigned.com  (Detached)
    16956.tattooremo    (Detached)
    1082.refinedwater.co.uk (Detached)
    27256.apple.com (Detached)
    21481.careus.co.uk  (Detached)
    326.onlinebuziness.me.uk    (Detached)

# screen -ls | grep "Detached" | awk '{ print $1; }' | cut -d'.' -f2- | xargs -I {} -n 1 screen -S {} -X quit

# screen -ls
    No Sockets found in /var/run/screen/S-root.

Normal session names : when I just type "screen" to openup screen session

 #screen -ls
    There are screens on:
    11580.pts-0.server  (Detached)
    11571.pts-0.server  (Detached)
    2 Sockets in /var/run/screen/S-root.

# screen -ls | grep "Detached" | awk '{ print $1; }' | cut -d'.' -f1 | xargs -I {} -n 1 screen -S {} -X quit

# screen -ls
No Sockets found in /var/run/screen/S-root.

Solution 3

How about something like this:

screen -ls | awk -F. '$NF~"(Attached)" {print "kill -HUP " $1}' | sh

Leave out the | sh if you want to see what it's going to execute.

It seems to work fine in a quick test I did.

Share:
19,763
nitin
Author by

nitin

Updated on September 18, 2022

Comments

  • nitin
    nitin over 1 year

    I have been working in different screen sessions in past projects, so I have a few screen sessions accumulated. Now I have been asked to remove excess/unnecessary screen sessions from the Unix box. None of them are dead sessions - the status is detached, not dead. The OS is Solaris.

    One of the methods that came to my mind is to delete the folder where screens are kept in the /tmp/mylogin/screen-r/... folder, but am not sure if that would leave any extra processes or something passive on the server.

    How can I remove them without leaving anything behind?

    Also, do these detached screens occupy quite a bit of resources, enough to alert the sysadmin? That is, are there actually any issues created by having a few unused/detached screen sessions around?

    • demure
      demure almost 11 years
      Have you considered attaching them, and exiting...? or are you asking for a fast way to kill them all?
    • Hans Meiser
      Hans Meiser almost 11 years
      sounds like this is the same question stackoverflow.com/questions/1509677/…
    • nitin
      nitin almost 11 years
      @demure i dont use them now , i made a new one for each project and now have quite a few .......... just thinking of ways to get rid of them or at least reduce them!!
    • nitin
      nitin almost 11 years
      @HansMeiser ....... thanks , i did search for it , but didnt find anything here. I didnt look in SO. Any way to close this question or should i just delete this ?
    • Kruug
      Kruug almost 11 years
      @HansMeiser to be fair, that one should probably have been migrated to SU...