2

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?

  • 1
    Have you considered attaching them, and exiting...? or are you asking for a fast way to kill them all? – demure Jun 3 '13 at 23:58
  • 2
    sounds like this is the same question stackoverflow.com/questions/1509677/… – Hans Meiser Jun 4 '13 at 0:01
  • @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 Jun 4 '13 at 1:09
  • @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 ? – nitin Jun 4 '13 at 1:12
  • @HansMeiser to be fair, that one should probably have been migrated to SU... – Kruug Jun 4 '13 at 17:54
2

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.

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.
1

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.
0

I think the secure way is first kill screen process then run screen command with the option I added.

#ps  -ef|grep screen|grep  -v  grep 

root  8362     1   0 21:20:38 ?           0:00 screen


# screen -ls
There are screens on:
        8363.pts-19.   (Dead ???)
        8262.8  (Attached)
Remove dead screens with 'screen -wipe'.
2 Sockets in /root/.screen.

#kill -9 8262

# screen -wipe
There are screens on:
        8362.pts-19   (Removed)
        8263.8  (Removed)
2 sockets wiped out.
0

I had this issue with a user. I first ran this command to see how many screen sessions were open by that user:

*# ps aux | grep username > /detached_screens.txt**

(The above command will look at the processes that are running and the grep command will help you narrow down the search to only show processes ran by that specific user. After grep username, I had it write to a file for reference. You don't have to use that part) I found 278 detached screen sessions that had never been closed from previous months. After figuring out the PID for each detached screen, I ran the following command:

*# kill -9 PID PID PID PID PID ...

This killed all the processes I specified.

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.