Timeout not working for SSHFS connection in script


This Content is from Stack Overflow. Question asked by Rudolf

I want to automate a sftp-based backup in a alpine-based docker container.
I’ve got a script that should check if the connection is established successfully.
Clients connect via ssh keys, so it is passwordless authentication.

timeout -k 1 4 sshfs -p $port -o IdentityFile=/home/ssh/ssh_host_rsa_key,StrictHostKeyChecking=accept-new,_netdev,reconnect $user@$address:/ /mnt/sftp/

This line establishes the sftp connection. It works just fine if the key is correct and even when the server is refusing the connection. But there is a problem when the server isn’t accepting the key provided, then it’s asking for a password in the interactive shell like:
user123@backup.example.xyz's password:
and timeout just does not kill the process, the script doesn’t go foroward after this since it waits for user input (which is not going to happen).

I use this script at startup to check the connection and stop the container immediately if it fails, so the user notices configuration errors right as he starts the container.

Is there a way to kill this command after a certain time or as a workaround prohibit interactive input for the sshfs command?



This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?