The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inline function inside Classes deepthi.s High Level Programming 1 08-22-2008 02:47 PM
value inside a function trichyselva Shell Programming and Scripting 2 08-04-2008 08:44 AM
calling function inside awk jisha Shell Programming and Scripting 2 04-14-2008 08:44 AM
remsh inside of while loop joettacm UNIX for Advanced & Expert Users 1 12-07-2007 12:54 PM
read inside a while loop dta4316 UNIX for Dummies Questions & Answers 3 05-21-2005 11:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-09-2008
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
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 (permalink)  
Old 09-09-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
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 (permalink)  
Old 09-10-2008
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Quote:
Originally Posted by otheus View Post
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 (permalink)  
Old 09-10-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-10-2008
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Thumbs up

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...
  #6 (permalink)  
Old 09-10-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-10-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
Quote:
Originally Posted by era View Post
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:32 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0