5

I'm trying to set up a script that opens a terminal, does an ssh to a remote server, and executes a command (tail -F logfile in my case).

What I have so far is the following

gnome-terminal -e 'ssh -t server "tail -F logfile"'

This works to some extent. -t ensures that signals like SIGINT are sent through the commands running remotely.

However, when I ctrl-c the tail, I would really like to drop down to a bash terminal on the remote server. Right now, if I ctrl-c the tail, then the tail is closed, which causes ssh to exit, which causes the whole terminal to be closed.

What I would like is for the tail to be terminated and be left with a bash shell on the remote server.

I have tried the following:

gnome-terminal -e 'ssh -t server "tail -F logfile; /bin/bash"'

but that doesn't seem to work. That is, if I run this without gnome-terminal, just ssh -t ..., then see the following:

some lines
from the log
^CConnection to server closed.

But, If I do

gnome-terminal -e 'ssh -t server "nonexistantcommand; /bin/bash"'

Then I get an error that nonexistantcommand is not found, and then I do drop down to a bash on a remote server...

Does anyone have any suggestions or hints on how to get this going? Thanks in advance.

3 Answers 3

2

Here, have a nasty hack:

gnome-terminal -e 'ssh -t server "echo \"tail -F logfile;rm /tmp/foo\" > /tmp/foo; bash --rcfile /tmp/foo"'
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to work. Thanks :) chx has more of an explanation in his answer as to why
1
--init-file filename
--rcfile filename

Execute commands from filename (instead of `~/.bashrc') in an interactive shell.

so run bash with --rcfile pointing to a script running tail -F.

Comments

1

Why not do the obvious? "If you got a SIGINT, execute an interactive shell."

gnome-terminal -e 'ssh -t server "trap \"exec sh -i\" INT; tail -F logfile"'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.