'How I can directly login to rails console through ssh?

Time to time I repeat the following commands:

ssh username@servername
cd /projects/rails_project
bundle exec rails c production

I want to create a shell script, and make alias for this file for run production console in one line. If i wrote simple script whith this 3 command it is dosen't work.

How I can do it?



Solution 1:[1]

Just send it as an argument:

ssh username@servername 'cd /projects/rails_project && bundle exec rails c production'

From man ssh:

SYNOPSIS
       ssh [options] [user@]hostname [command]
       ...

       If command is specified, it is executed on the remote host instead of a 
       login shell.
       ...

       When  the  user's identity has been accepted by the server, the server
       either executes the given command in a non-interactive session or, if no
       command has been specified, logs into the machine and gives the user a
       normal shell as an interactive session.  All communication with the remote
       command or shell will be automatically encrypted.

Solution 2:[2]

If you don't use ssh -t, the Rails console will not prompt you for input. What you actually want is:

ssh -t username@servername "cd/projects/rails_project && bundle exec rails c production"

Using -t " [...] can be used to execute arbitrary screen-based programs on a remote machine [...]" (according to the ssh man page.)

Solution 3:[3]

To be more precise, if you use bash and have error of bundle command not found then you have set it before running bundle exec, like:

ssh -t <user>@<server> "cd /home/appfolder && /bin/bash --login bundle exec rails c -e production"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andreas Louv
Solution 2 Eddie Rubeiz
Solution 3 alexey_the_cat