Linux – Clean up orphaned screen sessions for defunct users

gnu-screenlinux

I've run into the problem where I have users logging in to servers under customer accounts, opening a screen session as that user, and then never closing it. Ordinarily this wouldn't be much of a concern, however we're running into an issue where if a customer is migrated off the server [which deletes their user account] with an active screen session under their username that screen session sits idle. While that's still not much of a problem there is yet another step in this clustercuss.

If a client that was previously resident on a server, which has an old screen session still active, is migrated back to the server screen refuses to start because /var/run/screen/S-username still exists and is owned by the old UID, and the user [with a spanking new UID] isn't allowed to touch any of these files in their named directory.

I've tried:

  • Simply killing the screen processes, but that does not clean up /var/run/screen/S-username.
  • To find some combination of screen arguments that will let me gracefully kill these sessions, but screen does not want to let me touch anything but the current user's sessions, even if I'm logged in as root.
  • sudo -u '#uid' screen -r but sudo will not let you do anything if you specify a nonexistant UID.

How can I make screen shut down properly and clean up all of its files when the user no longer exists?

Best Answer

If you're fine with killing them off and really just want a way to deal with the unowned sockets sitting in /var/run/screen, I would just set up a cron job using find with the -nouser flag to clean up the old files:

# touch owned notowned && chown 12345:54321 notowned
# ls -l
total 0
-rw-r--r-- 1 12345 54321 0 Sep 27 21:59 notowned
-rw-r--r-- 1 root  root  0 Sep 27 21:59 owned
# find . -nouser
./notowned
#
Related Topic