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.

Basically what I want to do is something like this:

screen -session-name=mydefaultscreen

and if the 'session name' of mydefaultscreen doesn't exist, it creates it. if it does exist already then it reattaches to it.

I know i can do screen -x $somenumberhere but i want it all automatic

thanks

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Are you running GNU tools on a windows system or are we talking about linux systems? I'll assume the later, but let me know if I asume incorrectly.

Look at the man pages for the "reconnect" options screen offers. If my memory serves, "-r" and "-R" are the ones you will be looking for and there is a difference between them. I'll need some more details about what you are experiencing to completely describe the way to "automatically" do what you asked, but in short it might require a small script to accomplish. That would be for the more complicated case. If you have a simpler usage scenario and requirements, then you could simply put a line or two into the appropriate shell hook (e.g. if you use the bash shell, then .bash_profile, .bashrc, ... will execute/evaluate their contents depending on the scenario).

Are you getting dropped or timed-out of your session often? If so, there may be some SSH options that need to be tweaked too. What you are asking is do-able, I just need a better understanding of the parameters that the solution must be designed under.

share|improve this answer
add comment

In your .bashrc file, you could check if $SSH_CONNECTION (which describes whether the current user is logged in via SSH) is set and TERM not "screen" (which is the case if you run it from within screen), alternatively check the value of $SHLVL (which describes the level of nested shells).

Something like the following in your .bashrc should do to automatically start screen if you log in via SSH:

if [ -n "${SSH_CONNECTION}" -a ${SHLVL} -eq 1 ]; then
    screen -RD
fi

It should re-attach you to the last screen session or create a new one if necessary. Note that other users of that session will be detached. You might also want to have a look at the various other permutations described in the manpage.

   -d -r   Reattach a session and if necessary detach it first.
   -d -R   Reattach a session and if necessary detach or even create it first.
   -d -RR  Reattach a session and if necessary detach or create it. Use the first session if more than one session is available.
   -D -r   Reattach a session. If necessary detach and logout remotely first.
   -D -R   Attach  here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first.  If it was not running create it and notify the user. This is the author's favorite.
   -D -RR  Attach here and now. Whatever that means, just do it.

Of course, you could also just set screen as your login shell.

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.