Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have been reading about how to automatically start a screen when I ssh to a remote server. I would like a simple one-liner to add to my .bash_profile to start a screen when I ssh in, and NOT exit my remote connection when I detach the screen.

I have been looking into exec screen and pretty much every combination of -d -r -D and -R and cannot figure out what flags I need.

Ideally I would type ssh whatever and be logged into the server in a new screen or it can reattach an old screen, I would kind of like to know how to do both to see which I like better. Then I can either type ctrl a,d or ctrl d and have it bring me back to the normal ssh login for that server (where you would see the motd). From there I can screen -r back into whatever screen I want, or hit ctrl d again to log out.

share|improve this question

migrated from stackoverflow.com Apr 9 '13 at 1:55

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers 2

up vote 7 down vote accepted

A simple screen -R should do the trick.

To verify this, I added screen -R to my .bash_profile on a remote server, logged in, detached from screen, and was dropped back to a normal shell prompt on the remote server. Verified with .bashrc as well. Subsequent logins yielded the expected result (re-attach to screen session).

share|improve this answer
    
Thanks! I was reading around and I was under the impression that I would need exec screen -R. Glad this worked –  Ronald Dregan Apr 9 '13 at 3:48
    
You're quite welcome. Enjoy! –  btanaka Apr 9 '13 at 4:21
add comment

If I understand your need, you should try :

if $(screen -ls | grep -q pts); then screen -x; else screen -R; fi

It will create only one screen session, otherwise it will reconnect to the existing one in a multi-windows fashion ( even if you have multiple ssh session, you will end up to the same screen session )

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.