• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Jeff Mikels

…biblical Christianity without conservative idolatry…

  • Home
  • All My Sermons
  • My Blog
    • Longer Articles
    • Christian Leadership
    • Spiritual Health
    • Tough Questions
    • Geekery
    • All Posts
  • About Me
    • My Books
    • My Church
    • FAQ
  • Show Search
Hide Search
Home/Geekery/Bash // Create or Attach to Screen Session on Login

Bash // Create or Attach to Screen Session on Login

I use GNU Screen for everything I do in Linux… perhaps you do too, but perhaps you are annoyed that every time you log in to a terminal session on your Linux machine, you have to go through the one extra step of reconnecting to your screen session or checking to see if one is already running and then maybe creating a new session.

Here’s a way to make sure whenever you SSH into your Linux server, you always get right back where you were in your previous screen session just like you left it. The added bonus of this is that whenever you detach from your screen session, you are also automatically logged out of the server.

Simply add these lines to the bottom of your .bash_profile (watch out for word wrapping).

# start screen session if not already started
# or connect to screen session if not already in it
echo ''
echo '----------- WE LIKE SCREEN ------------------------------'
if [ ${TERM:0:6} != "screen" ]
then
    echo "Attempting to connect/create screen session."

    # We don't want to forcibly disconnect other sessions if they are
    # Attached, so we check for Detached sessions first
    HAVE_DETACHED=$(screen -list | grep Detached)
    HAVE_ATTACHED=$(screen -list | grep Attached)

    if [ -n "$HAVE_DETACHED" ]
    then
        echo "Attaching to existing screen session"
        exec screen -r

    elif [ -n "$HAVE_ATTACHED" ]
    then
        echo "Existing screen sessions are all attached"
        echo "use 'screen -rd' to detach and attach here."
    else
        echo "There are no running screen sessions."
        echo "Creating new screen session."
        exec screen
    fi
else
        echo "Already in a screen session. Cool."
fi

Written by:
Jeff Mikels
Published on:
March 28, 2015
Thoughts:
2 Comments

Categories: Geekery

Previous Post: « Hide Desktop for Distraction Free Writing
Next Post: Questions: The Shroud of Turin »

Reader Interactions

Comments

  1. Zach

    November 23, 2015 at 10:23 am

    A pastor who uses Linux? That’s awesome!

    Are you using Screen for scrollback history and multitasking in one terminal, or is there another benefit I’m not seeing in the man page? I’ve been using multiple SSH connections to my home server, but now I’m thinking Screen is a better solution. Especially when my kids get on my home PC and close my terminal sessions.

    Reply
    • Jeff Mikels

      December 2, 2015 at 11:16 am

      The biggest benefit that screen provides is persistence. I can launch a process in a screen session, and if my SSH connection is dropped, the process continues, and I can reconnect at any other time to continue right where I left off. Secondly, in the servers I maintain, I have a screen session constantly running with about 7 “windows” active. I have a couple windows for my local user and a couple windows viewing log files with the tail -f command. It makes my SSH sessions as convenient as the local virtual terminals would be if I were physically at the computer, and it all is restored to me as soon as I connect.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Articles

Dear Christian Conservative: Vote Your Conscience

Today is Super Tuesday meaning that a large number of states are having their primaries for the Presidential election. Currently, for …

Continue Reading about Dear Christian Conservative: Vote Your Conscience

Let’s Talk about Salvation: Part 2

Salvation and Forgiveness in the Gospel of John In my previous post, I considered a large number of passages in the synoptic gospels (Matthew, …

Continue Reading about Let’s Talk about Salvation: Part 2

Let’s Talk About Salvation and Forgiveness: Part 1

I've been thinking a lot about this idea recently: What would it look like if we were as eager to forgive as Jesus was? This is a tough …

Continue Reading about Let’s Talk About Salvation and Forgiveness: Part 1

Evangelical Idolatry

I've been going through a dilemma. The question on my mind is this: How do I talk about my book with people who don't know me all that …

Continue Reading about Evangelical Idolatry

Explore more

Footer

About Me

Jeff Mikels • 765-404-0807

Copyright © 2025 · Log in

  • Home
  • Blog
  • About Me
  • FAQ
  • Contact