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:
# Add to the bottom of ~/.profile
# Check if this is being called from an interactive shell
case "$-" in
# Great, it's an interactive shell. Is this shell being stated by sshd?
# Thanks to "Peter from Bavaria" for pointing out that readlink /proc/$PPID/exe
# no longer works as non-root and sugguesting the more portable ps solution
if [ "X$(ps -p $PPID -o comm=)" = "Xsshd" ]
# exec screen. Attach to or create a new session called auto_ssh.
# -x allows multiple copies of screen to be attached to the same session.
exec screen -xR auto_ssh