ssh script doesn't return control to the parent script

无人久伴 提交于 2019-12-11 01:56:56

问题


I am trying to execute a local script on the remote server, by writing it to standard input of an ssh command. The script runs fine, but then ssh doesn't exit: it just hangs, and control doesn't return to the parent script.

Parent Shell :

for HOSTNAME in ${HOSTS} ; do
    ssh -t -t $HOSTNAME "bash -s" < ~backup_conf.sh 
done

Called Script:

#!/bin/sh    
AGENT_BASE_PATH=/home/lokesh

if [ -d "$AGENT_BASE_PATH/CI/DE_deployment/conf" ]; then 
        if [ -d "$AGENT_BASE_PATH/CI/temp/conf_bkup" ]; then 
            rm -rf $AGENT_BASE_PATH/CI/temp/conf_bkup
        fi  
        cp -R $AGENT_BASE_PATH/CI/DE_deployment/conf $AGENT_BASE_PATH/CI/temp/conf_bkup
fi
exit

I have written 'exit' but the control is not returning back to the parent script. It hangs at the remote server.... :(


回答1:


Culprit is bash -s line in your calling code, which is still expecting input to be ended using ctrl-C:

Try this instead:

for HOSTNAME in ${HOSTS} ; do
    ssh -t -t $HOSTNAME "bash ~backup_conf.sh"
done



回答2:


write your exit - status into a file on the remote host and pick it later from the remote host with ssh/scp/sftp.

Direct via ssh you will not get a exit - status from the other host.



来源:https://stackoverflow.com/questions/22481371/ssh-script-doesnt-return-control-to-the-parent-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!