[英]Remote execution of SSH command hangs in ruby using Net::SSH for a particular command
I found a similar question here , but the answer in such a question didn't work for me.我在这里找到了一个类似的问题,但是这样一个问题的答案对我不起作用。
I am trying to connect the remote ssh server via ruby using Net::SSH.我正在尝试使用 Net::SSH 通过 ruby 连接远程 ssh 服务器。
It is working fine for me for all the commands provided via script and I could read the output of the command successfully.通过脚本提供的所有命令对我来说都很好,我可以成功读取命令的输出。
But when I use the below command it is getting stuck in SSH.exec!(cmd) and control is not returned from the line.但是当我使用下面的命令时,它会卡在 SSH.exec!(cmd) 中,并且不会从该行返回控制。 Only if i click Ctrl+c in command line the script is getting ended.只有当我在命令行中单击 Ctrl+c 时,脚本才会结束。
sudo -S su root -c 'cockroach start --advertise-addr=34.207.235.139:26257 --certs-dir=/home/ubuntu/certs --store=node0015 --listen-addr=172.31.17.244:26257 --http-addr=172.31.17.244:8080 --join=34.207.235.139:26257 --background --max-sql-memory=.25 --cache=.25;'
This is the script I run from a SSH terminal with no issue:这是我从 SSH 终端运行的脚本,没有问题:
sudo -S su root -c 'pkill cockroach'
sudo -S su root -c '
cd ~;
mv /home/ubuntu/certs /home/ubuntu/certs.back.back;
mkdir /home/ubuntu/certs;
mkdir -p /home/ubuntu/my-safe-directory;
cockroach cert create-ca --allow-ca-key-reuse --certs-dir=/home/ubuntu/certs --ca-key=/home/ubuntu/my-safe-directory/ca.key;
cockroach cert create-node localhost 34.207.235.139 172.31.17.244 $(hostname) --certs-dir /home/ubuntu/certs --ca-key /home/ubuntu/my-safe-directory/ca.key;
cockroach cert create-client root --certs-dir=/home/ubuntu/certs --ca-key=/home/ubuntu/my-safe-directory/ca.key;
'
sudo -S su root -c 'cockroach start --advertise-addr=34.207.235.139:26257 --certs-dir=/home/ubuntu/certs --store=node0015 --listen-addr=172.31.17.244:26257 --http-addr=172.31.17.244:8080 --join=34.207.235.139:26257 --background --max-sql-memory=.25 --cache=.25;'
This is the Ruby script who attempts to do exactly the same, but it gets stuck:这是尝试做完全相同的 Ruby 脚本,但它卡住了:
require 'net/ssh'
ssh = Net::SSH.start('34.207.235.139', 'ubuntu', :keys => './plank.pem', :port => 22)
s = "sudo -S su root -c 'pkill cockroach'"
print "#{s}... "
puts ssh.exec!(s)
s = "sudo -S su root -c '
cd ~;
mv /home/ubuntu/certs /home/ubuntu/certs.back.#{rand(1000000)}};
mkdir /home/ubuntu/certs;
mkdir -p /home/ubuntu/my-safe-directory;
cockroach cert create-ca --allow-ca-key-reuse --certs-dir=/home/ubuntu/certs --ca-key=/home/ubuntu/my-safe-directory/ca.key;
cockroach cert create-node localhost 34.207.235.139 172.31.17.244 $(hostname) --certs-dir /home/ubuntu/certs --ca-key /home/ubuntu/my-safe-directory/ca.key;
cockroach cert create-client root --certs-dir=/home/ubuntu/certs --ca-key=/home/ubuntu/my-safe-directory/ca.key;
'"
print "Installing SSL certifications... "
puts "done (#{ssh.exec!(s)})"
s = "sudo -S su root -c 'cockroach start --advertise-addr=34.207.235.139:26257 --certs-dir=/home/ubuntu/certs --store=node0015 --listen-addr=172.31.17.244:26257 --http-addr=172.31.17.244:8080 --join=34.207.235.139:26257 --background --max-sql-memory=.25 --cache=.25;'"
print "Running start command... "
puts "done (#{ssh.exec!(s)})"
# Use this command to verify the node is running:
# ps ax | grep cockroach | grep -v grep
s = "ps ax | grep cockroach | grep -v grep"
print "#{s}... "
sleep(10)
puts "done (#{ssh.exec!(s)})"
ssh.close
exit(0)
Here is the put put of the ruby script:这是 ruby 脚本的 put put:
C:\code2\blackstack-deployer\examples>ruby start-crdb-environment.rb
sudo -S su root -c 'pkill cockroach'...
Installing SSL certifications... done ()
Running start command...
As you can see, the command gets stuck in the line Running start command...
如您所见,该命令卡在Running start command...
行中
I tried putting the command in the background:我尝试将命令放在后台:
s = "sudo -S su root -c 'cockroach start --advertise-addr=34.207.235.139:26257 --certs-dir=/home/ubuntu/certs --store=node0015 --listen-addr=172.31.17.244:26257 --http-addr=172.31.17.244:8080 --join=34.207.235.139:26257 --background --max-sql-memory=.25 --cache=.25 &'"
print "Running start command... "
puts "done (#{ssh.exec!(s)})"
but what happned is that the cockroach process never starts (the ps ax | grep cockroach | grep -v grep
returns nothing)但发生的事情是cockroach进程永远不会开始( ps ax | grep cockroach | grep -v grep
什么都不返回)
I figured out how to fix it.我想出了如何解决它。
I added > /dev/null 2>&1
at the end of the command, and it worked.我在命令末尾添加了> /dev/null 2>&1
,它起作用了。
cockroach start --background --max-sql-memory=.25 --cache=.25 --advertise-addr=%net_remote_ip%:%crdb_database_port% --certs-dir=%crdb_database_certs_path%/certs --store=%name% --listen-addr=%eth0_ip%:%crdb_database_port% --http-addr=%eth0_ip%:%crdb_dashboard_port% --join=%net_remote_ip%:%crdb_database_port% > /dev/null 2>&1;
Are there any logs/output in the cockroach-data/logs
directory (should be located where you are running the start
command from). cockroach-data/logs
目录中是否有任何日志/输出(应该位于您运行start
命令的位置)。
Or perhaps try redirecting stdout+stderr to a file and seeing if there is any output there.或者也许尝试将 stdout+stderr 重定向到一个文件并查看那里是否有任何输出。
My hypothesis is that the CRDB process isn't starting correctly and so control isn't being returned to the terminal.我的假设是 CRDB 进程没有正确启动,因此没有将控制权返回给终端。 The cockroachdb docs say that the --background
flag only returns control when the crdb process is ready to accept connections. cockroachdb 文档说--background
标志仅在 crdb 进程准备好接受连接时返回控制。 And the question/answer you linked noted that "SSH.exec! will block further execution until the command returns".您链接的问题/答案指出“SSH.exec!将阻止进一步执行,直到命令返回”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.