There a few systems that I frequently work on from multiple locations. I like to be able to log back in and pick up where I left off after disconnecting and screen is great for that, but I have to remember to start it before I do anything else. After forgetting one too many times, I figured out how to start it automatically when I open an interactive SSH session. Here’s what I came up with:

1# Add to the bottom of ~/.profile
2# Check if this is being called from an interactive shell
3case "$-" in
4*i*)
5# Great, it's an interactive shell. Is this shell being stated by sshd?
6# Thanks to "Peter from Bavaria" for pointing out that readlink /proc/$PPID/exe
7# no longer works as non-root and sugguesting the more portable ps solution
8if [ "X$(ps -p $PPID -o comm=)" = "Xsshd" ]
9then
10 # exec screen. Attach to or create a new session called auto_ssh.
11 # -x allows multiple copies of screen to be attached to the same session.
12 exec screen -xR auto_ssh
13fi
14esac