command timeout in ksh

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting command timeout in ksh
# 1  
Old 09-07-2012
command timeout in ksh

I probably read all the threads in almost all the forums for a solution to my need. I am a beginner in shell scripting and I dont have a perfect solution yet. Below is my code snippet.
idql -n $REPOSITORY_NAME.$cs -Udmadmin -P"" -R$DM_SCRIPTS/test.api > /dev/null 2>&1 
    if [ $? != 0 ]; then 
      echo "   \c" 
      echo "ERROR: Cannot connect to: $REPOSITORY_NAME.$cs on $HOST" 
      echo "   Successfully connected to: $REPOSITORY_NAME.$cs" 

This is from the main logic that we use for monitoring our service. But we often see our service getting hung and so the first line of the above snippet gets hung and it doesnt proceed after that. Due to this we are not able to catch this 'service hung' condition.

Most importantly we have to retain the checks for the existing conditions (specified in the if-else conditional statements) and additionally we have to be checking for the 'hung' state. If the idql command takes more than 5 seconds, we can assume that it is hung.

Could you please help me achieve this.
# 2  
Old 09-07-2012
i dont know idql but many connectivity tools such as ssh, scp and others have timeout option in them which can be use as a leverage. can you try

idql --help

# 3  
Old 09-07-2012
idql --help

Thanks for your reply.

idql --help
syntax: idql<docbase> [-U<user>] [-D<domain>] [-P<password>] [-K<secure flag>] [ -R<input file>] [-n] [-l<error level>] [-X] [-z] [-w<display width>]

There is no in-built timeout option. We have to manually timeout the command after say 5 seconds.
# 4  
Old 09-07-2012
i dont know any other method but this probably is worth a try. If your script is named run it like below, sleep 10 is 10 seconds after the script started, after that it will be killed. Once its killed you can be sure it timedout. lets see if other folks have more idea.

-bash-3.2$ ./ & sleep 10 ;pkill
[2] 26221
[2]+  Terminated              ./

# 5  
Old 09-07-2012
Thanks ryandegreat25.

In this case, how to capture the result of the command, i.e, if the command is not hung, how to know whether it was a success or failure.
if [[ $?!=0 ]] will be true even if its a usual failure condition or a service hung condition, right?

I want to be able to catch 3 different conditions separately and print error messages correspondingly.
# 6  
Old 09-07-2012
Not sure if this is the best way to do this Smilie you might want to create a script inside a script.

if successful, pkill wont be able to kill any which will result to exit code 1 else it will be 0

-bash-3.2$ ./ & sleep 10 ;pkill
[1] 28058
[1]+  Done                    ./
-bash-3.2$ echo $?

in your case you might want to put idql in script1 and then the conditional test in script2, though im afraid it cant capture failed or success. Smilie maybe some conditional test again in script1.

Just to add the normal execution will be 0 and failure will be something else, the kill only happens if it hang so this is where the 2nd script comes in.

Last edited by ryandegreat25; 09-07-2012 at 05:38 AM..
This User Gave Thanks to ryandegreat25 For This Post:
# 7  
Old 09-07-2012
Anybody has any other solutions please?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Timeout to abolish ssh connection command it takes too long

Hi, I am running a ssh connection test in a script, how can I add a timeout to abolish the process if it takes too long? ssh -i ~/.ssh/ssl_key useraccount@computer1 Thank you. - j (1 Reply)
Discussion started by: hce
1 Replies

2. Shell Programming and Scripting

Using awk command in ksh

I am trying to use awk command withing ksh. ksh "echo \"my name\" | awk '{print $2}'" I am getting out as : my name Expected output: name When I use echo "my name" | awk '{print $2}' I am getting the correct output: name I am not sure what mistake I am doing when using awk... (1 Reply)
Discussion started by: Bala_db2
1 Replies

3. Shell Programming and Scripting

Mailx command timeout Issue

Hi All, I have a situation where we need to send out mails from our Unix server to different mailing ids inside our scripts.The mails are working fine.But we have occasional issues where we are getting time out errors from the SMTP server while sending out mails. Command that we are using... (3 Replies)
Discussion started by: DevotedPupil
3 Replies

4. Shell Programming and Scripting

Command timeout from inside script.

Hi, I've written a very robust script to get an external IP address from 'behind' a router. It uses many web pages randomly choosing which one/ones to use at run time. The "fetch the web page containing the IP address" is handled by either wget or curl both of which have their 'max time for the... (6 Replies)
Discussion started by: gencon
6 Replies

5. Shell Programming and Scripting

php shell_exec, exec command timeout

HI, Does anybody know if its possible to execute a command through exec, shell exec, system and if the program doesn't terminate in N seconds returns control to PHP ? reg, research3 ---------- Post updated 10-16-09 at 12:20 AM ---------- Previous update was 10-15-09 at 11:03 PM... (1 Reply)
Discussion started by: research3
1 Replies

6. Shell Programming and Scripting

KSH script SQL timeout issue

Hi all, I have a KSH script which is kicking off an sql scripts as follows: /usr/local/installs/instantclient_10_2/sqlplus -s username/password @$sql_path/sql_query.sql > $tmp_path/sql_query_results The problem I have is that sometimes the 10g Oracle Database spits out an error saying... (4 Replies)
Discussion started by: Donkey25
4 Replies

7. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

8. Shell Programming and Scripting

Can we timeout cd command

Hi All, I want to know whether we can timeout the cd command in unix. If we can how is it implemented? Suppose cd command hangs can we timeout the command. Please help (9 Replies)
Discussion started by: dipashre
9 Replies

9. Shell Programming and Scripting

How to timeout the "read" command

I have a script that at some point will ask the interactive user a question: #!/bin/ksh echo "What is your access code?" read ans ... Sometimes this script is run by other scripts and there are no interactive users. The script then hangs on the "read" command, waiting for a user response... (5 Replies)
Discussion started by: rm-r
5 Replies

10. Shell Programming and Scripting

ksh read timeout

any idea on how to timeout the read statement for ksh? for bash u can use read -t option -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has ... (2 Replies)
Discussion started by: ashterix
2 Replies
Login or Register to Ask a Question

Featured Tech Videos