1

I'm trying to create a single script that connects to a remote server and executes an update, but I want to make sure no one is using the machine at the time so I execute a "ps" command to see the process and then prompt if the installation should continue. Here's the code I'm attempting to make:

ssh -T root@$machine <<- 'END'
    #Show the non-root processes
    ps -ef | grep -v root

    #Prompt if should continue (this doesn't work)
    echo "Continuar a instalação? [y/n]"
    select valor in y n
    do
        case $REPLY in
            y )
                echo "Digitado y"
                break
                ;;
            n )
                echo "Digitado n"
                exit
                ;;
        esac
    done

    #Test if it's working
    echo "Test"
END

When I execute this code, I get the following behaviour:

Continuar a instalação? [y/n]
1) y
2) n
#? 1) y
2) n
#? #? 1) y
2) n
#? #? 
#? 1) y
2) n
#? #? #? 1) y
2) n
#? #? #? #?

And then the script closes. I tried running ssh with the -t -t option but I still get the same problem. What am I missing?

  • remote command execution using ssh will not have a tty assigned to the process. Hence it will not be able to accept input. – MelBurslan Apr 12 '16 at 14:24
  • And what can I do about it? – JefersonFG Apr 12 '16 at 14:26
  • There are several suggestions HERE ON THIS QUESTION. One of them might help you – MelBurslan Apr 12 '16 at 14:54
2

You can't communicate with the remote process (select), when you put the script on stdin. It happens to you that the select reads some garbage from your stdin and then reads EOF, when your input ends.

You should rather copy the file to the server using scp and then run it.

0

I decided to separate the command into two ssh connections, one for listing the processes and other to update, and in the middle of them the prompt to continue:

ssh -T root@$machine <<- 'END1'
    ps -ef | grep -v root | grep -v lightdm
END1

echo "Continuar a instalação? [y/n]"
select valor in y n
do
    case $REPLY in
        y )
            break
            ;;
        n )
            exit
            ;;
    esac
done

ssh -T root@$machine <<- 'END2'
    printf "Test\n"
END2

The problem is that it'll require you to put the password for the ssh connection twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.