Limiting the hanging of a function inside a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Limiting the hanging of a function inside a loop
# 1  
Old 09-09-2008
Limiting the hanging of a function inside a loop

#! /bin/ksh

..(some lines are here..)

for t in (LIST)
do
a=function($t)

#here I want to wait till the function hangs for 5 secs else want to continue the loop for other values of t.

done


if [ $a = 1 ];then
.............
fi
......
......................

Suppose in this script; inside the for loop the function() hangs and unable return the result; then I want to wait till 5 secs. If the function hangs more than 5 secs for a certein variable then I want to continue the loop for the other variables in the LIST .

plz help...
# 2  
Old 09-09-2008
You run your function in the background. After that starts, you sleep 5 seconds, then kill it with %1. Then call wait.

Code:
date
function($t) &

# get job number
x=`jobs -p`
# kill that job in 5 seconds
{ sleep 5; kill $x; } &>/dev/null &

# wait for something to finish
wait $x
date

The wait will complete when all sub-processes have finished. The kill will silently fail if the job completes first.

Hope this helps.
# 3  
Old 09-10-2008
Smilie
Quote:
Originally Posted by otheus
You run your function in the background. After that starts, you sleep 5 seconds, then kill it with %1. Then call wait.

Code:
date
function($t) &
 
# get job number
x=`jobs -p`
# kill that job in 5 seconds
{ sleep 5; kill $x; } &>/dev/null &
 
# wait for something to finish
wait $x
date

The wait will complete when all sub-processes have finished. The kill will silently fail if the job completes first.

Hope this helps.
=======================
Thanks a lot...
I tried this as follows.. But it is not working..
-------------------------------------------------
#scipt name: fun
f()
{
enccp -c mas start service_name -v -myPwd password
# This is a IBM-encina command that is creating the sevice.. Which is hanging in this case
}

f &
x=`jobs -p`
{ sleep 1; kill $x; } &>/dev/null &
wait $x
date

OUTPUT:
------------------------------------------------------
[/home2/niroj_p]./fun
Wed Sep 10 11:28:59 IST 2008
[/home2/niroj_p]starting service_name
retrieving attributes of object service_name ... done
setting up the server resources
retrieving node object mars6 ... done
creating file system path /var/opt/encina/local/subsys/encina/css6/server/service_name... exists
retrieving encina server group name ... done
retrieving encina administration organization ... done
.................
...........................Then it is hanging as usual
# 4  
Old 09-10-2008
Can you kill it with a regular kill from the command line? If not, does it respond to other signals? (The default signal is SIGTERM, usually signal 15. Other useful signals to try are 2 = SIGINT and -- in utter desperation only -- 9 = SIGKILL.) Ctrl-C usually sends SIGINT.
# 5  
Old 09-10-2008
MySQL

I tried this program with using for loop...

=========================================
set -x

f()
{

enccp -c mas start serv_nm -v -myPwd encina_admin

}

i=0
for i in 1 2
do
f &
x=`jobs -p`
{ sleep 1; kill -9 $x; } &>/dev/null &
wait $x
date
i=`expr $i + 1`
done

===========================================

OUTPUT:
-------------

[/home2/niroj_p]./fun
+ i=0
+ + f
+ enccp -c mas start serv_nm -v -myPwd passwd
+ jobs -p
x=0
+ sleep 1
+ 1> /dev/null
+ wait 0
+ date
Wed Sep 10 13:57:24 IST 2008
+ + expr 1 + 1
i=2
+ f
+ + enccp -c mas start serv_nm -v -myPwd passwd
+ + jobs -p
x=0
0
0
+ sleep 1
+ 1> /dev/null
+ wait 0 0 0
+ date
Wed Sep 10 13:57:24 IST 2008
+ + expr 2 + 1
i=3
[/home2/niroj_p]starting serv_nm
retrieving attributes of object serv_nm ... done
setting up the server resources
retrieving node object mars6 ... done
creating file system path /var/opt/encina/local/subsys/encina/css6/server/serv_nm ... exists
retrieving encina server group name ... done
retrieving encina administration organization ... done
starting serv_nm
retrieving attributes of object serv_nm ... done
setting up the server resources
retrieving node object mars6 ... done
creating file system path /var/opt/encina/local/subsys/encina/css6/server/serv_nm... exists
retrieving encina server group name ... done
retrieving encina administration organization ... done
+ kill -9 0 # Kill is working nice

=====================
After this I have to enter any key to come out from the process.
That I want to avoid..


Thank u friends...Smilie
# 6  
Old 09-10-2008
Are you sure you really must use kill -9? It can be dangerous. Try with kill -2 first.

The proper (or at least customary, and easiest) way to get the process ID is with x=$! right after starting the background job.

I don't understand the for loop, do you start two separate processes and sleep a maximum of one second between them? I'm not certain about the need to press a key after the process is killed; are you sure it's not the second round of the loop?
# 7  
Old 09-10-2008
Quote:
Originally Posted by era
The proper (or at least customary, and easiest) way to get the process ID is with x=$! right after starting the background job.
Cool! I never knew that one. That's why I come here. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

3. Shell Programming and Scripting

If loop inside function not working.

check_deplver () { dir=/abc/def/ghi if ssh -o StrictHostKeychecking=no $1 "" 2> /dev/null then echo " output is " ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc" 2> /dev/null else echo " directory not presnt" fi } This is not working. But... (7 Replies)
Discussion started by: NarayanaPrakash
7 Replies

4. Programming

Changing value inside function

I have the following code and want to update shiftDesc inside the function. Is it correct to declare the argument as: int shiftDesc void prValue_vd( FILE* stream, // name of output stream int shift, // amount of shift to the right const char* value,... (1 Reply)
Discussion started by: kristinu
1 Replies

5. Shell Programming and Scripting

PERL - press Enter function hanging

Hi, I have a sub function called pressEnter within my perl menu script which basically means that the script will await an interaction from the user before moving on. Unfortunately when the script goes into pressEnter it just hangs (even if you press enter!!). Any ideas on what could be... (4 Replies)
Discussion started by: chris01010
4 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. HP-UX

Limiting SFTP Users While Not Limiting Regular Users?

Hi, I have searched the web and have come back with nothing that is satisfactory for what I require. SFTP is my corporations new file transfer standard. What I require is a method to lock down SFTP users to their directory (they may go to sub directories) while not restricting regular users. ... (2 Replies)
Discussion started by: Emancipator
2 Replies

9. Shell Programming and Scripting

Function's return value used inside awk

I have a file with the record of person: cat > $TMP/record.txt John Torres M Single 102353 Address Mark Santos M Maried 103001 Address Carla Maria F Maried 125653 Address #!/bin/ksh ManipulateID(){ ... return 0; ... #or return 1; } cat $TMP/record.txt | awk 'BEGIN {printf... (4 Replies)
Discussion started by: Orbix
4 Replies

10. Shell Programming and Scripting

value inside a function

i have a script like this #!/bin/ksh initialize() { x=20 } ... ... ... x=10 initialize; echo $x (2 Replies)
Discussion started by: trichyselva
2 Replies
Login or Register to Ask a Question