67

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.

quanta
  • 52,423

5 Answers5

90

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.
39

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.
Alakdae
  • 1,233
8

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.

w00t
  • 615
4

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
Brent
  • 24,065
0

the accepted answer works for executing a simple command but not for executing somewhat more complex commands (for example multiple commands using the "&&" operator). In that case it may be needed to invoke the command using "sudo" and a shell invocation, e.g.:

ssh -t root@my.machine sudo -- "screen bash -c 'test -e /var/log/messages && tail -f /var/log/messages'"

or only using a shell invocation:

ssh -t root@my.machine 'screen bash -c "test -e /var/log/messages && tail -f /var/log/messages"'

you may also consider adding an "exec bash" at the end to keep the session open afterwards, e.g.:

ssh -t root@my.machine 'screen bash -c "test -e /var/log/messages && tail /var/log/messages ; exec bash"'

if you just want to create the remote screen session and immediately detach from it afterwards you may also consider using the screen options "-d -m", e.g.:

ssh -t root@my.machine sudo -- "screen -S test123 -d -m bash -c 'test -e /var/log/messages && tail -f /var/log/messages'"

you can later on reattach to the remote screen session using:

ssh -t root@my.machine 'screen bash -c "screen -x test123 ; exec bash"'

The screen option "-S test123" is used to name the session and option "-x test123" to reattach to the session.

cripton
  • 101