1

I am using gem net-ssh.

The connection is established. I am able to run some simple scripts(like "pwd", "ls", etc.) successfully, but when I run the "restart" script I get an error.

Here is a snippet:

output = ssh.exec!("cd /home/csrhub/git/csrhub-api; ./bin/restart")
puts "#{output}"

And the output:

./bin/restart:7:in exec': No such file or directory - bundle (Errno::ENOENT) from ./bin/restart:7:in' Environment determined as "development" based on the .environment file. Loading application.yml

And here is the script where the error occurs.

#!/usr/bin/env ruby

require 'yaml'
require_relative '../app/models/environment.rb'
config = Environment.config('application')

exec "bundle exec pumactl --control-url tcp://127.0.0.1:#{config['control_port']} --control-token #{config['control_token']} phased-restart"

Connecting via Putty, when I run the same command it gets executed without any problems.

EDIT

The PATH variable is different when I Putty:

/home/csrhub/.rbenv/shims:/home/csrhub/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

And when I run with net-ssh PATH is:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

8
  • Don't cd -- Just run /home/csrhub/git/csrhub-api/bin/restart Commented Jul 23, 2019 at 16:23
  • @Zak Same, still doesn't work. I get the same error :( Commented Jul 23, 2019 at 16:25
  • Check what is in the .bashrc on your server, it might have something ruby-specific. Replicate that to .bash_profile. There's a difference between running interactively and non-interactively. Commented Jul 23, 2019 at 17:10
  • @philpirozhkov docs.google.com/document/d/… docs.google.com/document/d/… Commented Jul 23, 2019 at 17:50
  • Seems like a PATH issue. The PATH for your ssh-net shell session is not the same as for your Putty session. This may be the reason the bundle command can not be found. Commented Jul 23, 2019 at 17:54

1 Answer 1

2

The problem is in your ssh.exec! call. Net::SSH does not use a login shell to run your commands. A login shell is required for Bash to execute .bash_profile.

Since your rbenv setup is in .bash_profile, your exec! call will be missing your full Ruby environment, and therefore running bundle later in you restart script will fail.

The solution should be to manually execute .bash_profile, so that rbenv gets loaded:

output = ssh.exec!("source ~/.bash_profile; ...rest of commands...")

More information and other proposals here:
net-ssh and remote environment

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.