Find and reattach a lost terminal screen session under Linux / Mac OS X

Running lengthy operations (such as remote sync, video compression) is preferred to be done in a screen session: among other things, it enables you to do something else while your commands run in the background. For the most part you can be fairly sure that you can attach to your session anytime you want. However, there comes a time when for some reason your previously started screen session disappears. This has been reported under Mac OS X as if you leave the detached screen alone for an extended period of time, it can become terminated (or disowned by your user) automatically. In times like this the only thing that helped me are the following steps.

Note: I’m assuming you have brew.sh up and running (if you don’t then by any means do it) and you have installed htop by issuing  brew install htop  in your Terminal.app – I agree that there are more sophisticated ways of sending KILL signals to processes but having htop around is nice.

If the usual screen session listing only produces an error message stating you have no lock files, like this:

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

Then by all means follow along as we try to make it recreate the missing socket!

First, dive in and find the actual screen process among the currently running processes. Open up a Terminal and run this:

ps aux |grep screen

if you are not getting any output then try with capital letters like this:

ps aux |grep SCREEN

You should see something like this:

username          3217   0.0  0.0  2460736    368   ??  Ss   Wed09PM   0:10.10 SCREEN -t conv

the second column (here it’s “3217”) is the important process ID (or pid for short) we’ll be looking for. If you have multiple screen sessions then be sure to note the pid of the one that you currently have no access to (hence lost).

Now, we need to fire up htop by running this:

htop

You’ll be presented with a summary of resources along with a lengthy list of running processes. On your keyboard hit Fn + F3 (if you have multimedia keys enabled – if not, just press F3) to activate Search mode and start typing “screen”. The cursor should jump to the first screen session – make sure you select one that has the same pid as the one from before.

Finally, we are going to force the screen session to recreate the socket by sending itt a SIGCHLD signal. While your desired screen session with the right pid is selected, press Fn + F9 (or just F9) to bring up the Kill Menu within htop. Here go down to number 20 which says SIGCHLD and hit Enter to Send the process that signal.

This should force it to recreate the fifo socket so if you run the command to reattach ( screen -r <pid> ) then it should now bring up your lost screen session without a single error.

Alternatively, if you do not want to use htop then a simple kill command does the very same – for the sake of completeness, here it is using the pid from above:

kill -CHLD 3217

For more info and additional help check out the screen FAQ over here.

About the author

Gábor Hargitai

View all posts