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?


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

  • 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 (( – Eugen Konkov Mar 1 '16 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). – Jakuje Mar 1 '16 at 8:50
  • have you mispell, the man does not describe -tt, only -t? – Eugen Konkov Mar 1 '16 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. – Gombai Sándor Mar 1 '16 at 9:45
  • 1
    I see, I just took "When I connect remote I always run..." literally. – Gombai Sándor Mar 4 '16 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.