How to kill a dead screen session? - linux

Originally posted in Unix and Linux community but no correct answer after a couple of days. I will move the answer there if I get the answer from here. Thanks!
How do I kill a dead screen session?
[allen#mmbp ~]$ screen -ls
There is a screen on:
3634.wb_backend (Dead ???)
Remove dead screens with 'screen -wipe'.
1 Socket in /var/run/screen/S-allen.
I tried screen -X -S 3634.wb_backend quit and here is the result:
There is a screen on:
3634.wikibrain_backend (Dead ???)
Remove dead screens with 'screen -wipe'.
No screen session found.
I tried screen -S -X wb_backend kill and here is the result:
mkfifo /var/run/screen/S-allen/27272.-X failed
This (Dead ???) session used to be an Attached session and I used:
screen -X -S 3634.wb_backend kill
to kill it. It ended up to be in this weird status.
I have tried:
screen -wipe
but it didn't work.
How could I terminate it?

screen -wipe PID
will wipe the dead screen.

You need to kill the PID as reported by screen -ls, then wipe the session:
kill -9 3634
screen -wipe

You can send a kill command to the specific screen session.
E.g.
[allen#mmbp ~]$ screen -ls
There is a screen on:
3634.wb_backend (Dead ???)
Remove dead screens with 'screen -wipe'.
1 Socket in /var/run/screen/S-allen.
You would just kill the specific PID
kill 3634

Problem solved. It ended up to be a insufficient disk space problem - the hard disk went into read-only mode.

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

Related

How to write bash script to re-attach to a existing linux screen?

I want to write a bash script to re-attach to the existing linux screen, perform some commands and then detach from that screen. I know to create a new linux screen in detached mode and perform some commands. But I haven't found a way to re-attach to a existing linux screen.
You can give a name to a screen session by using the -S option.
Example : screen -dS NAME <command>
Then you can reattach to that session using screen -r NAME
try this;
user#host:/screen -ls
There are screens on:
29229558.pts-5.host (Detached)
46661728.pts-22.host (Detached)
user#host:/screen -r 46661728.pts-22.host
If you see (attached) as below;
user#host:/screen -ls
There are screens on:
29229558.pts-5.host (Detached)
46661728.pts-22.host (Atached)
screen -D -r 46661728.pts-22.host
To re-attach to the screen you have to follow this steps:
Before de-attaching from the screen, execute:
STDOUTTERM=$(who -m|awk '{print $2}')
Then you can attach stdout to whatever you want. When you finish, to reattach to stdout, execute this:
exec > /dev/$STDOUTTERM
You can send a command to a running screen using screen -X exec. See the man page for redirection options.
screen -X exec ls
Alternatively you can send text to a running screen's stdin buffer using screen -X paste, as if the user had typed it. This can be a way of running commands inside an interactive shell.
screen -X register a "ls\n"
screen -X paste a

Using screen in startup script

I'm trying to write a startup script to launch rtorrent. I've planed on using screen in the script. I tried this command in the startup script :
su -l nico -c "screen -dmS rtd rtorrent"
But it seems like it doesn't work (i can't get the screen session back) :
screen -ls
No Sockets found in /var/run/screen/S-nico.
Do i miss something ?
The list of active screen sessions is by default maintained independently per user.
In your screen launch command, the session was started by user nico, so if you want to inquiry about it you should probably run su -l nico -c 'screen -ls' instead.

Kill Attached Screen in Linux

I created a screen "myscreen" in linux and it stopped responding abruptly. I closed the terminal and tried to reopen it. "screen -ls" shows that the screen is Attached. I tried the following commands but nothing responds.
screen -r myscreen
screen -D myscreen
screen -D -RR myscreen
screen -X -S myscreen quit
Any help to reattach to the screen or to kill the screen is very much appreciated.
screen -X -S SCREENID kill
alternatively, you can use the following command
screen -S SCREENNAME -p 0 -X quit
You can view the list of the screen sessions by executing screen -ls
Create screen from Terminal:
screen -S <screen_name>
To see list of screens:
<screen -ls> or <screen -list>
To go to particular screen:
<screen -x screen_name>
<screen -r screen_name>
Inside screen
To Terminate screen:
give ctrl+d screen will get terminated
To Detach screen:
give <ctrl+ad>or <screen -d >screen will get detached
To reattach screen:
screen -x <screen_name> or screen -r <screen_name>
To kill a screen from Terminal:
<screen -X -S screen_name quit>
or
<screen -X -S screen_name kill>
You can use screen_name or process_id to execute commands.
This worked for me very well. Get the screen id via:
screen -r
or
screen -ls
then kill the screen: kill -9 <screenID>
it now becomes a dead screen,
then wipe it out with: screen -wipe
To kill a detached screen use this from the terminal:
screen -X -S "SCEEN_NAME" quit
If you are attached, then use (from the terminal and inside the screen):
exit
From Screen User's Manual ;
screen -d -r "screenName"
Reattach a session and if necessary detach it first
You could create a function to kill all existing sessions. take a look at Kill all detached screen sessions
to list all active sessions use
screen -r
when listed, select with your mouse the session you are interested in and paste it. like this
screen -r
Suppose your screen id has a pattern. Then you can use the following code to kill all the attached screen at once.
result=$(screen -ls | grep 'pattern_of_screen_id' -o)
for i in $result;
do
`screen -X -S $i quit`;
done
i usually don't name my screen instances, so this might not be useful, but did you try screen -r without the 'myscreen' part? usually for me, screen -r will show the PIDs of each screen then i can reattach with screen -d -r <PID>
You can find the process id of the attached running screen.
I found it same as the session id which you can get by command:
screen -ls
And you can use following command to kill that process:
kill [sessionId] or sudo kill [sessionId]
None of the screen commands were killing or reattaching the screen for me. Any screen command would just hang. I found another approach.
Each running screen has a file associated with it in:
/var/run/screen/S-{user_name}
The files in that folder will match the names of the screens when running the screen -list. If you delete the file, it kills the associated running screen (detached or attached).
For result find: Click Here
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows.

How to stop a screen process in linux?

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?
CTRL+a and then 'k' will kill a screen session.
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.
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.
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 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

Running shell script in detached screen session. Must kill. How?

I am an Ubuntu Linux user. I am running jobs remotely and started a screen session. During this session I sourced a file containing a long list of command lines arguments to be executed. I was forced off of the connection and now the jobs are still running in this screen and I am unable to kill them.
Does anyone know how to kill all running and future commands this script will execute. Thank you in advance.
If you just want to kill everything there is no need to even reattach to screen.
Just list the offending process(es):
pstree -pla
then kill whatever needs killing. Note that if you kill a process higher up the process tree, its children will (usually) go away as well.
Reattach the screen with
screen -D -r
then you can resume your session.
Use ps to identify the pid of the shell process (bash, tcsh, etc), then kill that...
There are a couple of 'screen' ways to kill a specific screen session ...
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.

Resources