Your problem problem appears to be in this part of the command
sudo su another_user ; cd /some/dir ; RAILS_ENV=production bundle exec rails console
This invokes three commands after each other. Each one of them will wait for completion of the previous command.
The first command sudo su another_user
will start a shell as another_user. Once you leave that shell, the following commands will be executed.
The commands cd /some/dir ; RAILS_ENV=production bundle exec rails console
will be run as user who you logged in as. If you expected those commands to run as another_user, and they don't behave as you intended, when they are run as user, then that will explain your problems.
Perhaps you meant to write
sudo su another_user -c "cd /some/dir ; RAILS_ENV=production bundle exec rails console"
or
sudo su - another_user -c "cd /some/dir ; RAILS_ENV=production bundle exec rails console"
where the extra -
causes environment variables to be updated and login scripts to be run as another_user before the rest of the command is executed.
Answer which I wrote for a previous revision of the question
For better security and ease of use, I recommend you run both ssh
commands on the client host and none on the jump server. The combination of ProxyCommand
and -W
arguments is perfectly suited for that.
ssh -o ProxyCommand='ssh -W %h:%p jump-server' -t app-server 'cd /some/dir ; rails console'
The -t
argument and command should cover the rest of your requirements.