How can I launch a screen session with a command over ssh on a remote server from my desktop?

79,372

Solution 1

Try using the -t option to ssh

ssh -t [email protected] 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.

Solution 2

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.

Solution 3

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.

Solution 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
Share:
79,372

Related videos on Youtube

Thomas Vander Stichele
Author by

Thomas Vander Stichele

Updated on September 17, 2022

Comments

  • Thomas Vander Stichele
    Thomas Vander Stichele over 1 year

    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 [email protected] 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.

    • Goblinhack
      Goblinhack about 9 years
      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.
  • Thomas Vander Stichele
    Thomas Vander Stichele almost 15 years
    Your answer was equally good, too bad I have to choose. Maybe someone should edit the first answer and add your information.
  • Kjetil Joergensen
    Kjetil Joergensen over 11 years
    @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.
  • Tarun
    Tarun over 8 years
    I tried this, but when I do screen -ls then I don't see any screen session
  • dragon788
    dragon788 almost 7 years
    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.
  • w00t
    w00t almost 7 years
    @dragon788 indeed, I made it a function now and replaced $MYSERVER with "$@". I still use this one a lot.
  • zhangjinzhou
    zhangjinzhou almost 7 years
    I am not using ssh but is facing the same issue. Please help. serverfault.com/questions/861632/unable-to-run-code-in-scree‌​n
  • Ruslan
    Ruslan over 3 years
    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).
  • Diamond
    Diamond almost 3 years
    Hwo can we choose a name for that screen as well?
  • Diamond
    Diamond almost 3 years
    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".