How to stop a screen process in linux?

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:

  • Sending ctrl-c to specific screen session
  • $'\003' is ctrl+c http://donsnotes.com/tech/charsets/ascii.html
  • stuff is https://www.gnu.org/software/screen/manual/screen.html#Paste

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.


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.


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