screen: no more losing my vim editing sessions when the ssh connection drops
screen is a utility that lets interactive shell and application processes persist between login sessions. It also lets you manage multiple sessions within a single login session and between login sessions. There’s a ‘windows’ analogy beggin to be made, but it wouldn’t be quite accurate or useful.
I’m a bit embarrassed, because I’ve known about screen for quite some time, and I just started really using it. Maybe it took me so long to adopt because it has one of the longest man pages I’ve ever seen (which, btw, is a good thing, albeit a tad intimidating). In any case, that is one friggin long man page, so I’ll enumerate some of the basics here — a cheat sheet, if you will:
Create a new screen session named ‘codingMarathon’:
screen -S codingMarathon
‘Detach’ your current session so you can log out of ssh and shut your computer down, for instance:
ctrl-A d
‘Retach’ the same session by name:
screen -r codingMarathon
List all screen sessions:
screen -ls
Screen can do a lot. You can ‘nest’ screens. That is, once you are inside a screen session, you can open new ‘windows’ with: ctrl-A c and jump between the windows of a screen session with the mnemonics ctrl-A n (next) and ctrl-A p (previous).
Don’t forget this one. ctrl-A ? gives a quick list of available commands.
Since we use suPHP, I need to edit each site as a different user. That’s a good thing, for security reasons, but that’s a lot of login sessions to maintain! On our development machine, I am only using one login, and maintaining screen sessions for each vhost’s user. I begin them like so:
screen -S vhostName su vhostuser
I’ve been doing this for about a week, and I have screen sessions that are that old. I’m guessing they’ll last until that machine goes down, which will probably be a while!
Even if I didn’t have to use a bunch of different logins, screen would still be a boon. Twice already, I’ve had network interruptions kill my ssh connection in the middle of a coding session. With screen, I could pick up right where I left off! I know that, for some people, interruptions come more than once a day. You know who you are. Try adjusting the keep-alive interval in your ssh client. And use screen!
A word of caution… always save your work. Screen is great, but you’ll waste time doing file recoveries if you detach unsaved editing sessions and the server has to go down.
This is certainly no replacement for R-ign The F-ing M. man screen is, as with most man pages, going to help you the most.




