How to stop a screen process in linux?

LinuxGnu Screen

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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.

Solution 3 - Linux

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.

Solution 4 - 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:

Solution 5 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - LinuxNikView Answer on Stackoverflow
Solution 2 - LinuxtroyfolgerView Answer on Stackoverflow
Solution 3 - LinuxbiocybermanView Answer on Stackoverflow
Solution 4 - LinuxmgearView Answer on Stackoverflow
Solution 5 - LinuxdíyuanluView Answer on Stackoverflow