How to exit a shell script if a unix command does not return any value for 10 seconds?

Hi,

Can anyone help me how to exit a shell script if a unix command inside does not return any value for 10 seconds?

The scenarios is like this.

I want to login to a application using shell script where the connection string is mentioned.but suppose this connection string is not responding means after passing the password it is getting hanged, not getting into that application.In this case i want the connection string to wait for sometime say 10 seconds and after that exit the shell program.

Any idea how to do this?:wall:

What have you tried so far? What kind of client are you connecting with? What options does this client offer?

Does this 'connection string' happen to be ssh? Or maybe something else that has an option for connection timeout?

You can kill a program after 10 seconds, but do you want it dead in 10 seconds if the login was successful? How would you know?

After successful login a welcome message comes.
If it does not come then it needs to exit the script.

It should wait for 10 seconds if that message comes within that time then its ok else exit the script.

---------- Post updated at 08:10 PM ---------- Previous update was at 07:59 PM ----------

This is what i have tried so far

#!/bin/sh
# input timeout
mysql -user abc -passwordabc
read -t 5 password
if [[ $? -ne 0 ]]
then
  echo "Hanged"
else
  echo "success"
fi

I am trying to connect to mysql using shell script.

Why are you trying to read the password after running mysql? Wouldn't you need it before? And you're putting a password into mysql anyway.

yes it will read the password.
Need to remove that line.

how to check that it was able to establish the connection?

I want to exit the script if it was not able to establish the connection.
exit when it gets hanged.

If command/program has done correctly for *nix systems, it return 0 if okay and not 0 if there is some error. Usually command doc include comments for every exit codes.
Variable ? include last command exit status.

some_command
stat=$?
(( stat != 0 )) && echo "Some problem $stat" && exit 1

So test mysql command with correct values and not so correct values.
After command check exit status

echo $?

Hunting for timeout in man mysql I find it mentioned right below --var name=value:

       You can also set the following variables by using --var_name=value. The
       --set-variable format is deprecated.

       o  connect_timeout

          The number of seconds before connection timeout. (Default value is
          0.)

Ergo mysql --connect_timeout=10 ... sounds like what you want.

You may call the mysql in silent mode , i think its -s option.

Theny just trigger a simple select satement like select sysdate and redirect the output to a file.

After 10s check if this file is empty or not using wc -l.

Hi

Can anyone tell me if any command gets hanged in Unix what is its return code?

Is it a non -zero value?

A hanged command doesn't return. That's what hanged means.