22

I have multiple screens running on an Ubuntu server that are initiated as:

screen -dmS screen1 cmd
screen -dmS screen2 cmd
etc...

And I need to kill one screen, but not all of them. What is the correct command to kill a single particular screen with its name? I've read through the man pages but I can't seem to find the command I am looking for.

Also I want to write this command into a bash script so I can't simply screen -r screen1 then press Ctrl+X as I normally would.

chicks
  • 3,639
  • 10
  • 26
  • 36
BassKozz
  • 645
  • 2
  • 8
  • 15

6 Answers6

21

From the man page :

   -X   Send the specified command to a running screen  session.  You  can
        use  the  -d or -r option to tell screen to look only for attached
        or detached screen sessions. Note that this command  doesn't  work
        if the session is password protected.

You can do :

        screen -X -S <sessionid> kill
François Feugeas
  • 1,393
  • 9
  • 17
10

If you do a screen -list, you'll notice that each screen name begins with a number, which is the PID of the screen:

 $ screen -list
There are screens on:
        12281.pts-1.jonah       (12/21/2009 07:53:19 PM)        (Attached)
        10455.pts-1.jonah       (12/19/2009 10:55:25 AM)        (Detached)
2 Sockets in /var/run/screen/S-raphink.

From there, just send a KILL signal to this specific PID:

$ kill 12281

and it will kill the specific screen.

raphink
  • 11,337
  • 6
  • 36
  • 47
  • this won't work because I am running it from a bash script, and I rather not have to pull the PID from screen -list that matches the correct screen... defraagh's answer above worked like a charm. Thanks thou. – BassKozz Dec 21 '09 at 19:03
  • Ok, good that defraagh had a perfect solution for it. – raphink Dec 21 '09 at 20:05
2

If you have several screens with the same name, you can kill them at once:

screen -ls  | egrep "^\s*[0-9]+.ScreenName" | awk -F "." '{print $1}' | xargs kill
  • Command screen -ls prints screens with their process number. For example, 4773.test is a screen with process number 4773 and the name test. Sample output:

    6322.ss      (05/23/2018 10:39:08 AM)        (Detached)
    6305.sc  (05/23/2018 10:38:40 AM)        (Detached)
    6265.ScreenName       (05/23/2018 10:37:59 AM)        (Detached)
    6249.ScreenName  (05/23/2018 10:37:50 AM)        (Detached)
    6236.scc        (05/23/2018 10:37:42 AM)        (Detached)
    
  • Command egrep filters above sample text sent via piped line |.

  • Command awk -F "." '{print $1}' extracts first column of each line. Delimiter between columns is defined as dot (.) by option -F
  • Finally command xargs kill will kill all process whose numbers sent via pipe |. xargs is used when we want to execute a command on each of inputs.
Mohsen Abasi
  • 121
  • 2
1

defraagh's solution doesn't work for me, however I can kill the screen session using Raphink's idea:

screen -list get the process ID

kill -9 PROCESSID

screen -wipe SESSIONID

garconcn
  • 2,378
  • 7
  • 33
  • 46
1

Ive been dealing with this as follows:

process=$(screen -ls | grep screen1)
kill $(echo $process | cut -f1 -d'.')
exit

The explanation is that you take the output of screen -ls and find the particular screen you are interested in by using grep then assign that string to the variable process.

Since the output from screen -ls is always the PID followed by a period you can then use cut to get rid of the period and everything after it. in the above example we put that in parentheses and feed it to the kill command though you could do it linearly which might include writing to a temp file and reading out of that.

You need to make sure that your screen name is unique and that grep is ONLY returning the name of the screen you want to kill.

Rovanion
  • 569
  • 3
  • 6
  • 22
unifex
  • 11
  • 1
0

You open a new window with

Ctrl A + C

You close a window with

Ctrl + D

or exit command within the window