3

When I connect remote I always run

cd ~/w/logs
tail -f some_file.log

but when I do

ssh host 'cd ~/w/logs; tail -f some_file.log'

all is OK, but after CTRL+C the connection is lost. (similar question, but does not cover CTRL+C problem)

How to run command after remote login?

1 Answer 1

7

You need to do two things. The first is to run interactive bash after the tail will exit (the bash in the end) and then you need to allocate remote TTY so the session will become interactive (-t switches):

ssh -t host 'cd ~/w/logs; less +F some_file.log; exec $SHELL'

exec -- If command is specified, it replaces the shell. No new process is created

10
  • Nope. This does not work. When I press CTRL+C to stop 'tail' command the connection is broken. I have try this: ` ssh -t sv "cd ~/w/logs; exec bash -l -c tail -f psgi.log"` but without success (( Commented Mar 1, 2016 at 8:34
  • Ok, Ctrl+C, kills all session. Considering some commands that you don't have to kill (less) will work for you (will update answer). Commented Mar 1, 2016 at 8:50
  • have you mispell, the man does not describe -tt, only -t? Commented Mar 1, 2016 at 9:14
  • 1
    I just don't understand why you don't write your commands in the profile of your login shell and just start a simple ssh without any commands to run remotely. Commented Mar 1, 2016 at 9:45
  • 1
    I see, I just took "When I connect remote I always run..." literally. Commented Mar 4, 2016 at 8:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.