Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to exit a shell script if a unix command does not return any value for 10 seconds?
# 1  
Old 08-08-2012
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?Smilie
# 2  
Old 08-08-2012
What have you tried so far? What kind of client are you connecting with? What options does this client offer?
# 3  
Old 08-08-2012
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?
# 4  
Old 08-08-2012
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

Code:
#!/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.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Corona688; 08-08-2012 at 11:48 AM..
# 5  
Old 08-08-2012
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.
# 6  
Old 08-08-2012
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.
# 7  
Old 08-08-2012
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.
Code:
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
Code:
echo $?


Previous Thread | Next Thread
Test Your Knowledge in Computers #574
Difficulty: Medium
The process of allocating and deallocating memory is generally referred to as memory swapping.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux shell | how to exit a script if any command fails.

Hi, i am new here let me say HI for all. now i have a question please: i am sending one command to my machine to create 3 names. if one of the names exists then the box return error message that already have the name but will continue to create the rests. How i can break the command and... (7 Replies)
Discussion started by: Amiri
7 Replies

2. HP-UX

Find command doesn't return in shell script.

Hi All, I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file. However, this doesn't work as I believe the find command doesn't exit's... (11 Replies)
Discussion started by: Vipin Batra
11 Replies

3. Shell Programming and Scripting

Executing 'exit' command from shell script

Hi, I am writing shell script to automate few use cases for CLI interface. We have CLI interface which has bunch of commands. I am trying to execute one of the commands 'exit' as part of automation to exit from CLI object (not from shell script) in my shell script. My intension is to execute... (4 Replies)
Discussion started by: Mahesh Desai
4 Replies

4. Shell Programming and Scripting

Exit code from piping in unix shell script

Hi , I have following code in my shell script : "$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p1.stx" "${TS_LOGS}/tranfrmr_p1.err" | ( "$TS_BIN/cusparse" "${TS_SETTINGS}/cusparse_p2.stx" "${TS_LOGS}/cusparse_p2.err" | ( "$TS_BIN/tsqsort" "${TS_SETTINGS}/srtforpm_p3.stx"... (8 Replies)
Discussion started by: sonu_pal
8 Replies

5. Shell Programming and Scripting

store last command exit status in variable in shell script

Hello All My req is to store the exit status of a command in shell variable I want to check whether the file has header or not The header will contain the string DATA_ACQ_CYC_CNTL_ID So I am running the command head -1 $i | grep DATA_ACQ_CYC_CNTL_ID Now I have to check if... (6 Replies)
Discussion started by: Pratik4891
6 Replies

6. Shell Programming and Scripting

problem in exit status of the command in a shell script-FTP

Hi All, I have developed below script for FTP a file from unix machine to another machine. ftpToABC () { USER='xyz' PASSWD='abc' echo "open xx.yy.zbx.aaa user $USER $PASSWD binary echo "put $1 abc.txt" >> /home/tmp/ftp.$$ echo "quit" >> /home/tmp/ftp.$$ ftp -ivn <... (3 Replies)
Discussion started by: RSC1985
3 Replies

7. Shell Programming and Scripting

Exit script if the user dosent enter any data within 5 seconds

Hello friends, Kindly help me in developing a script that asks user to enter a value and will wait for 5 seconds for the feedback. If there is no answer from the user the script will perform exit or it will continue doing something else Ex: If yu have a multi OS system i believe while... (3 Replies)
Discussion started by: frozensmilz
3 Replies

8. Shell Programming and Scripting

command does not return exit status due to tee

Hi, I am using /bin/sh. I want to display the stdout and stderr on the terminal as well as save it in a file, so I'm using this command. gmake all 2>&1 | tee log But even if gmake fails, it's always giving 0 as exit status, i suppose because of tee. # false 2>&1 | tee Log # echo $? 0... (2 Replies)
Discussion started by: anand_bh
2 Replies

9. UNIX for Advanced & Expert Users

Move command return with exit code of 2

I have a script which loads data files into Oracle and then moves each file into a 'processed' directory when each file has finished loading. Last night I found that the script was failing on the mv statement (with a return code 2) and the following message, mv: cannot access... (1 Reply)
Discussion started by: handak9
1 Replies

10. UNIX for Dummies Questions & Answers

using exit command in a shell script

Can it be done? If so, how? I would like a script to contain the exit command, and log me off at script completion. thanks (1 Reply)
Discussion started by: jpprial
1 Replies

Featured Tech Videos