How to run command after ssh connection


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?

Best Answer

  • 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