6

Sometimes my script bugs out and multiple screen sessions with the same name get created. Is there a way to kill all of these with this name in one command?

Rx:/scripts# screen -S haydoz-script -X quit
There are several suitable screens on:
        13372.haydoz-script     (09/10/11 07:44:05)     (Detached)
        12287.haydoz-script     (09/10/11 07:30:45)     (Detached)
        12230.haydoz-script     (09/10/11 07:17:42)     (Detached)
        7824.haydoz-script      (09/09/11 11:42:26)     (Detached)
        7764.haydoz-script      (09/09/11 11:35:40)     (Detached)
        15753.haydoz-script     (09/04/11 15:00:27)     (Detached)
        15744.haydoz-script     (09/04/11 15:00:26)     (Detached)

There are other screens running besides these ones that I do not want to kill.

10

If there are no other screen sessions running you can use the "hard" way and just kill them with killall screen.

If you want to be nice you can iterate over your list of screen sessions and kill them one after another:

# screen -S foo && screen -S foo
[detached]
[detached]
# screen -ls
There are screens on:                                                                                                                                                                                      
        8350.foo        (Detached)                                                                                                                                                                         
        8292.foo        (Detached)                                                                                                                                                                         
2 Sockets in /tmp/screens/S-joschi.                                                                                                                                                                        

# This is the interesting line. Just replace "foo" with the name of your session(s)
# for session in $(screen -ls | grep -o '[0-9]*\.foo'); do screen -S "${session}" -X quit; done
# screen -ls
No Sockets found in /tmp/screens/S-joschi.
3
  • unfortunately, there are other screens that I don't want killed. – dukevin Sep 10 '11 at 8:39
  • That's why I also provided a "nice" version. ;) – joschi Sep 10 '11 at 8:41
  • The 'nice' version doesn't work for me – Jack Jul 18 '16 at 22:26
4

screen -ls "$SESSION_NAME" lists the full names of matching sessions on tab-indented lines. So you can iterate on these lines, extract the full names, and call the quit command on each matching name.

SESSION_NAME='haydoz-script'
screen -ls "$SESSION_NAME" | (
  IFS=$(printf '\t');
  sed "s/^$IFS//" |
  while read -r name stuff; do
      screen -S "$name" -X quit
  done
)
6
  • What is stuff? – Andy Ray May 12 '16 at 4:03
  • @user53145 The variable that contains words of the output from screen -ls after the first which is the session name. If you wrote read -r name stuff then name would contain the whole line (except the leading spaces), but we only want the first word. – Gilles 'SO- stop being evil' May 12 '16 at 7:30
  • What type of file is this? How do I run it. Typing it in console won't work – Jack Jul 18 '16 at 22:23
  • 1
    @JackNicholson This is a shell snippet. It should run on any sh-style shell (dash, bash, ksh, zsh, …). – Gilles 'SO- stop being evil' Jul 18 '16 at 22:52
  • Thank you I'm amazed your code worked! Had trouble with this for ages. Thank you. I ended up putting it in a .sh file and running sh kill.sh – Jack Jul 19 '16 at 8:11
0

Here is one-liner. You need to replace your_session with required one.

screen -ls your_session | grep -E '\s+[0-9]+\.' | awk -F ' ' '{print $1}' | while read s; do screen -XS $s quit; done

-2

Attach to screen and then run another screen command:

screen -x "Name" -x -X kill
1
  • There are multiple screens with this Name. So kill them by this command does not work! – Mohsen Abasi May 23 '18 at 10:39

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.