Note: You are viewing this blog post without the intended style information, which may result in formatting issues.

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Add to the bottom of ~/.profile
# Check if this is being called from an interactive shell
case "$-" in
*i*)
# 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" ]
then
  # 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
fi
esac