I want to execute some remote command on my server using net-ssh library.
I have the following example:
Net::SSH::start(host, user, options = {:keys => '~/.ssh/id_rsa'}) do |ssh|
puts ssh.exec!("echo $PATH")
ssh.loop
end
The result is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
My problem is that I have not my PATH loaded as it should be.
I should also have some RVM paths, and custom paths defining into my .zshrc.
How could I change this behavior to let net-ssh to use my .zshrc to load my default environment ?
Solution:
puts ssh.exec!("source ~/.zshrc; echo $PATH")
have you tried something like:
ssh.exec!("source /home/you/.zshrc")
puts ssh.exec!("echo $PATH")
?
Found a way around this problem, actually. It's quite nice solution. Thanks to Vald for telling me that every exec is independent of itself, so then you can just use && to link the commands together.
For example, I have a ruby script that is runnable, and I'm using zsh, so I just use this:
ssh.exec("source .zshrc && ./backup.rb")
Works like a charm! Of course you can also be on the safe side and use the full path or ~, but it gets the job done.
Edit: Sorry, also just saw you put a solution above. Is there a difference between using ;
and &&
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With