57

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.

  • 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. – Neil McGill Feb 25 '15 at 21:12
77

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.
  • 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 8: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 1 '12 at 0:34
  • I am not using ssh but is facing the same issue. Please help. serverfault.com/questions/861632/unable-to-run-code-in-screen – zhangjinzhou Jul 11 '17 at 21:23
32

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.
  • 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 9 '09 at 13: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.

  • 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 5: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
  • I tried this, but when I do screen -ls then I don't see any screen session – Tarun Oct 23 '15 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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