70

I am running a script on a remote server. I ran the script in screen, however I need to stop it before it completes since I need to update the script. I can easily detach from screen, however, is there a way to kill a screen process?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
David
  • 14,205
  • 20
  • 97
  • 144

5 Answers5

107

CTRL+a and then 'k' will kill a screen session.

Nik
  • 7,113
  • 13
  • 51
  • 80
  • Thank you! I just added a Terminal Hotkey that goes "\001ky" (Ctrl-A, k and y for "yes"). On a far note, I have a hotkey bound to the "Home" key that goes "cd ~/ Enter" There’s no place like cd ~/ – Andreas Feb 02 '23 at 01:39
70

There are a couple of 'screen' ways to kill a specific screen session from the command line (non-interactively).

1) send a 'quit' command:

screen -X -S "sessionname" quit

2) send a Ctrl-C to a screen session running a script:

screen -X -S "sessionname" stuff "^C"

In both cases, you would need to use 'screen -ls' to find the session name of the screen session you want to kill ... if there is only one screen session running, you won't need to specify the -S "sessionname" parameter.

troyfolger
  • 2,388
  • 2
  • 15
  • 14
8

I used this to quit hundreds of erroneous screen sessions created by a buggy command:

for s in $(screen -ls|grep -o -P "1\d+.tty"); do screen -X -S $s quit; done;

where: the grep -o -P "1\d+.tty" is the command to get session names with Perl-like name regex "1\d+.tty" which captures all sessions start with number 1, has some other numbers (\d) and end with .tty

Warning: You should test with this command first to see you get the exact list of sessions you want before apply the above command. This is to avoid quitting unwanted sessions:

for s in $(screen -ls|grep -o -P "1\d+.tty"); do echo $s; done;

I always to this echo test whenever the list in for loop is not clear, for example, the one generated by sub-command in $() expansion.

biocyberman
  • 5,675
  • 8
  • 38
  • 50
5

previous answers didn't work for me on a winputty terminal and amazon ssh server connection.. but this one does work:

screen -S yourscreentitlehere -X stuff $'\003'

references:

mgear
  • 1,333
  • 2
  • 22
  • 39
  • Awesome! This is what I was looking for! None of the other solutions would stop the screen session if it was currently running an active process. – Tom Chapin Jul 12 '19 at 18:32
2

I am using putty, and it seems I am already in the screen and couldn't open and close. Every time I do "exit", I just close the putty window. Here is the termimal print

>>screen -r

    21063.unlimited (11/08/20 15:45:19)     (Attached)
    24054.cure6     (11/08/20 09:46:13)     (Attached)

There is no screen to be resumed. and

screen -S 21063.unlimited -X stuff $'\003'

does not do anything. I found that as simple as the following line works perfect

screen -x 21063.unlimited

it sends me back into the screen and from there "exit" works. Note that it is lower-case -x

díyuanlu
  • 85
  • 1
  • 4