64

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
  • 50,327
  • 19
  • 152
  • 213
Thomas Vander Stichele
  • 1,055
  • 4
  • 14
  • 16
  • As an aside, you can hit this error not through bad usage of screen. I had a shell function that was doing exec < foo to read from a file. Much later in a large script I tried to launch screen. All the methods below obv. failed as the real culprit was the exec. One to bear in mind. Check you've not killed your own stdin inadvertently. – Goblinhack Feb 25 '15 at 21:12

4 Answers4

88

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.
Kjetil Joergensen
  • 5,854
  • 1
  • 26
  • 20
  • I am trying the same with tmux `ssh -t user@machine tmux attach` and I can attach a remote tmux session but it messes with the character encoding. It displays only ascii chars. Is there a fix for this? – Macario Jul 31 '12 at 08:22
  • 1
    @Macario you may want to open a separate question for this, while it's tangentially related as you're running it with ssh -t, it sounds more like it's related to setting LC_* differently for running interactively vs. non-interactively. When you pass a command-line to ssh to execute, it'll be executed with the non-interactive environment. It's also possible it's the lack of the TERM environment variable being passed when executing non-interactively, in which case tmux would have no idea what capabilities your terminal actually has, and falls back to the least common denominator. – Kjetil Joergensen Aug 01 '12 at 00:34
  • I am not using ssh but is facing the same issue. Please help. https://serverfault.com/questions/861632/unable-to-run-code-in-screen – zhangjinzhou Jul 11 '17 at 21:23
37

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,213
  • 8
  • 21
  • 1
    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 09 '09 at 13:57
  • 1
    This answer is better because it does detach and thus lets you continue working in this terminal (and to attach to the `screen` session from anywhere else). – Ruslan Aug 06 '20 at 19:07
  • Hwo can we choose a name for that screen as well? – Diamond Jun 25 '21 at 18:48
  • I am already ssh'ed to a remote server. From that shell, I want to shh to another server, When I run this command, it seems that it connects to the server (no error is printed), although after that, when I run "screen -ls", I get "No Sockets found in /var/run/screen/S-myname". – Diamond Jun 25 '21 at 18:57
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
  • 8
  • 14
  • 1
    I read through quite a few pages not quite answering what I was looking for but you nailed it. I'm assuming either screen or tmux is probably on most servers these days, and the ones I connect to are potentially ephemeral so I didn't want to mess with the bashrc like most answers suggest. You can also make this a function in your shell and accept arguments like $host and $command (which you would put at the end). And rather than hardcoding your alias to `t` which is a single host, you could combine it with hostnames in your ssh_config. – dragon788 Jun 16 '17 at 23:40
  • @dragon788 indeed, I made it a function now and replaced $MYSERVER with "$@". I still use this one a lot. – w00t Jun 17 '17 at 05:19
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
  • 22,219
  • 19
  • 68
  • 102