How can I launch a screen session with a command over ssh on a remote server from my desktop?
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
Related videos on Youtube
Thomas Vander Stichele
Updated on September 17, 2022Comments
-
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 about 9 yearsAs 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 almost 15 yearsYour answer was equally good, too bad I have to choose. Maybe someone should edit the first answer and add your information.
-
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 over 8 yearsI tried this, but when I do
screen -ls
then I don't see any screen session -
dragon788 almost 7 yearsI 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 almost 7 years@dragon788 indeed, I made it a function now and replaced $MYSERVER with "$@". I still use this one a lot.
-
zhangjinzhou almost 7 yearsI am not using ssh but is facing the same issue. Please help. serverfault.com/questions/861632/unable-to-run-code-in-screen
-
Ruslan over 3 yearsThis 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 almost 3 yearsHwo can we choose a name for that screen as well?
-
Diamond almost 3 yearsI 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".