ssh script doesn't return control to the paren

2019-07-17 05:07发布

问题:

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.