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.
Try using the -t option to ssh
From man ssh
You can use:
That starts a detached screen with a command running on it.
Late answer, but this is what I do, I make an alias (let's call it t) that does this:
This tells
ssh
to disable agent and X11 forwarding, and tellsscreen
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.
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: