I want to be able to launch screen sessions on remote servers from a single ssh command on my desktop. However, screen seems to need a terminal, which is not available when running a command through ssh.

So the obvious

ssh root@my.machine screen "tail -f /var/log/messages"

(as an example) does not work, and gives

Must be connected to a terminal.

I want ssh to launch the command under a screen so I can log in later and attach as I would to a screen session I would have launched manually.

link|improve this question

25% accept rate
feedback

4 Answers

up vote 31 down vote accepted

Try using the -t option to ssh

ssh -t root@my.machine screen "tail -f /var/log/messages"

From man ssh

-t      Force pseudo-tty allocation.  This can be used to execute arbi-
        trary screen-based programs on a remote machine, which can be
        very useful, e.g., when implementing menu services.  Multiple -t
        options force tty allocation, even if ssh has no local tty.
link|improve this answer
feedback

You can use:

ssh root@host screen -m -d "tail -f /var/log/messages"

That starts a detached screen with a command running on it.

   -m   causes screen  to  ignore  the  $STY  environment  variable.  With
        "screen  -m"  creation  of  a  new session is enforced, regardless
        whether screen is called from within  another  screen  session  or
        not.  This  flag has a special meaning in connection with the `-d'
        option:

   -d -m   Start screen in "detached" mode. This creates a new session but
           doesn't  attach  to  it.  This  is  useful  for  system startup
           scripts.
link|improve this answer
Your answer was equally good, too bad I have to choose. Maybe someone should edit the first answer and add your information. – Thomas Vander Stichele Jun 9 '09 at 13:57
feedback

Late answer, but this is what I do, I make an alias (let's call it t) that does this:

ssh $MYSERVER -a -x -t screen -xRR -A -e^Zz -U -O

This tells ssh to disable agent and X11 forwarding, and tells screen to attach to a running session, start a new one if needed, use ^Z as the breakout command, use UTF-8 and be smart about the terminal.

All this means that I can open a terminal, type t and it will open my screen session on $MYSERVER. I can then open another terminal, do the same thing and I get another window to the same session.

It's really nice to have multiple terminal windows to the same screen session so you get to look at two screens tabs at the same time.

link|improve this answer
feedback

By putting the following in the ~/.bashrc file on my server, it starts a screen session the first time I log on to the server, or if one is already running, re-connects me to that session.

I find this very handy:

if [ -n "$SSH_CONNECTION" ] && [ -z "$SCREEN_EXIST" ]; then
    export SCREEN_EXIST=1
    screen -DRi
fi
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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