Display screen sessions at login

Recently I've discovered how useful screen is. However after detaching from sessions, I keep forgetting about them. To solve that issue, I would like to display any active sessions after login.

The following code works perfectly, but am I handling it the right way - and in the correct file?

I've added this code to the end of ~/.bash_profile :
Code:
# Display if any screens are running after login
if [ "$TERM" != "screen" ]; then
   SCNS=$(screen -ls | awk '/[.]pts-/ {print $1}')
   if [ ! -z "$SCNS" ]; then
      echo "There are screens running on:" && echo "$SCNS" && echo ""
   fi
fi

Now it will show this at login if any screens are active:
Code:
FreeBSD 11.1-RELEASE-p4 (GENERIC) #0: Tue Nov 14 06:12:40 UTC 2017

Welcome to FreeBSD!

There are screens running on:
28921.pts-0.localhost

[name@localhost ~]$
 
Well, wouldn't you agree that the only "right" way is the one which gets you the required results? ;)

Not trying to be sarcastic mind you, but it's all so subjective. For example: running bash would be the 'wrong' way on my server(s), but that doesn't make it a wrong choice per definition. And this looks pretty slick to me, thanks for sharing!

One small comment... Depending on how you set up your server it is possible that the session won't be referred to as localhost:
Code:
unicron:/home/peter $ screen -ls
There is a screen on:
        1690.pts-0.unicron      (Detached)
1 Socket in /tmp/screens/S-peter.
Obviously that doesn't apply to you, but I figured I'd mention it anyway. I'd probably try something a bit more generic, maybe in the likes of screen -ls | cut -f2 | head -2 and just use the entire output.

Of course in the end I'm already fully used to running screen -x first time I log onto my server ;)
 
One small comment... Depending on how you set up your server it is possible that the session won't be referred to as localhost

You're absolutely right. It's better to regex for ".pts-". I've updated my post.

I like your example, but it will only display the first screen session (if more than one exists). My example will list them all.
 
Back
Top