2

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?

Gulaev Valentin
  • 575
  • 1
  • 7
  • 19

3 Answers3

5

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.)

Eddie Rubeiz
  • 53
  • 1
  • 4
4

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.
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • dosen't work ssh vgulaev@localhost 'cd /home/vgulaev/workspaces/insales_dev/insales && bundle exec rails c' bash: bundle: command not found – Gulaev Valentin Jun 10 '16 at 09:42
  • `bash: bundle: command not found ` do the server you ssh to have the `bundle` command? – Andreas Louv Jun 10 '16 at 09:44
  • whereis bundle bundle: /home/vgulaev/.rvm/gems/ruby-2.1.5/bin/bundle – Gulaev Valentin Jun 10 '16 at 09:51
  • @GulaevValentin And on the server? `ssh user@host 'whereis bundle'` – Andreas Louv Jun 10 '16 at 09:54
  • if I login to server it is say /home/vgulaev/.rvm/gems/ruby-2.1.5/bin/bundle – Gulaev Valentin Jun 10 '16 at 10:18
  • If I ssh user@host 'whereis bundle' it doesn't find – Gulaev Valentin Jun 10 '16 at 10:18
  • It's because you change your `.bashrc` on the server adds `/home/vgulaev/.../ruby-2.1.5/bin` to your PATH. But it properly also have a condition like: `[[ "$-" == *i* ]]`. Either make sure you update the PATH, or use the absolute path to `bundle`: `/home/vgulaev/.../ruby-2.1.5/bin/bundle rails c production` – Andreas Louv Jun 10 '16 at 11:02
  • ok. I use http://stackoverflow.com/questions/26247926/how-to-solve-usr-bin-env-ruby-executable-hooks-no-such-file-or-directory and now have problem with console:)) like http://stackoverflow.com/questions/11221345/rails-console-runs-without-prompt – Gulaev Valentin Jun 10 '16 at 12:27
0

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"
alexey_the_cat
  • 1,812
  • 19
  • 33