Linux – How to reconnect to a lost screen (detached, missing socket)

gnu-screenlinux

I had a screen session running on a home server. My workstation needed a reboot, so I detached and killed the terminal. Upon reconnecting to the server, I execute my typical

$ screen -D -R
[new screen]

Huh? No, not a new session, gimme the old one. I know, I'll grab it directly. What's the socket name?

$ screen -list
No Sockets found in /var/run/screen/S-username

$ ls -a /var/run/screen/S-username
.  ..

Wait… what? I know I left it running. Where'd it go?

$ ps -ef | grep -i screen
username  30860     1  0 Oct16 ?        00:00:29 SCREEN

Well, there's the process. But there's no socket to pass to screen -r. How can I reattach to my session?

Best Answer

Screen checks the fifo/socket whenever it receives a SIGCHLD signal. If the socket is missing, it will be recreated. So the solution is to find the process and send it SIGCHLD.

On my Debian system, screen appears to be installed as setgid utmp but not setuid, so the first solution from the FAQ below worked:

$ kill -CHLD 30860
$ ls /var/run/screen/S-username/
30860.pts-4.localhost

On systems where screen is installed setuid root, this won't work, and you'll need to kill one of the child processes of the active screen session to force the kernel to send the signal for you. This means sacrificing one of your screen windows to reconnect with the rest (choose wisely!).

From an archived Gentoo Wiki FAQ:

Socket Missing

Sometimes the socket of a still-running screen can be destroyed, though the actual process and all of its child processes are still running. screen -list will display "No Sockets found in /tmp/uscreens/.." Some handy instructions for how to recover from this (and a few other uncommon problems) at http://www4.informatik.uni-erlangen.de/~jnweiger/screen-faq.html#MISC about 2/3 of the way down.

Q: For some unknown reason, the fifo in /tmp/screens/S-myname is gone, and I can't resume my screen session. Is there a way to recreate the fifo?

A: Screen checks the fifo/socket whenever it receives a SIGCHLD signal. If missing, the fifo/socket is recreated then.

If screen is running non set-uid the user can issue a kill -CHLD screenpid directly (it is -CHILD on some systems). Screenpid is the process-id of the screen process found in a ps -x listing.

But usually this won't work, as screen should be installed setuid root. In this case you will not be able to send it a signal, but the kernel will. It does so, whenever a child of screen changes its state. Find the process-id (shellpid below) of the "least important" shell running inside screen. The try kill -STOP shellpid. If the fifo/socket does not reappear, destroy the shell process. You sacrify one shell to save the rest. If nothing works, please do not forget to remove all processes running in the lost screen session.

Related Question