在循环内的 bash shell 脚本中执行 ssh 命令 [英] Executing ssh command in a bash shell script within a loop
问题描述
我正在尝试在应执行以下操作的 bash shell 脚本中执行 ssh 命令:1)ssh到主机2)执行命令3) 命令的打印值4) 重复步骤 1 -35) 退出 bash shell 脚本
I'm trying to execute an ssh command within a a bash shell script that should perform the following: 1) ssh to host 2) execute command 3) print value of command 4) repeat steps 1 -3 5) exit bash shell script
我已经为远程主机设置了无密码进入,为远程主机添加了主机密钥
I have set up password less entry to the remote host, added host key to remote host
我想测试远程主机上运行的httpd进程的各种状态在文本文件 httpd_process.txt 中,我有:
I want to test the various states of the httpd process running on the remote host Within a text file, httpd_process.txt, I have:
/etc/init.d/httpd 状态(停止、启动、重启)
/etc/init.d/httpd status (stop, start, restart)
我在脚本中执行以下操作:
I do the following in the script:
while read LINE
do
echo "Httpd Request: $LINE"
status=`$LINE`
echo "Status: $status"
sleep 5 # sleep so that next
done < /path_name/httpd_process.txt
exit 0
我假设每次通过循环从输入文本文件中读取另一个输入字符串,并向远程主机发出请求.但是,我的经验是在第一个请求之后脚本终止.我是否正确假设在发送第一个请求时它会创建一个子进程,并且一旦该进程完成我的脚本完成并且不执行循环的下一轮?
I assumed that each time through the loop another input string is read from the input text file and the request is made to the remote host. However, what I experience is that after the first request the script terminates. Am I correct to assume that as the first request is sent it creates a child process and once that process completes my script completes and the next turn through the loop is not executed?
推荐答案
ssh
正在消耗标准输入.传递它 -n
以防止这种情况发生.
ssh
is consuming stdin. Pass it -n
to prevent this.
这篇关于在循环内的 bash shell 脚本中执行 ssh 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!