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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calling function inside awk jisha Shell Programming and Scripting 2 04-14-2008 04:44 AM
Help needed in function calling in a script jisha Shell Programming and Scripting 3 01-15-2008 03:48 AM
Calling a function ashika UNIX for Dummies Questions & Answers 5 09-12-2006 04:58 PM
Calling other file function maldini Shell Programming and Scripting 3 08-18-2005 11:23 PM
c++ calling main() function norsk hedensk High Level Programming 3 01-22-2003 04:28 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 338
Stumble this Post!
Question Help needed to Keep calling a function after every 5 seconds.

Hey guys.!
Need some help.I want to write a script , which should be continuosly running and should keep calling a function after every say 5 or 10 seconds.

I am done with almost all part of it, but figuring out how to keep the script continuosly running and how to keep calling a function after every 5 seconds.

Any help would be appreciated.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 30
Stumble this Post!
Thumbs up

Try the following:

func()
{
echo "you are in function"
sleep 5
func
}

This function will call itself itself again and again with a time delay of 5 sec.
Or if you want to call the function directly from main you can use any infinite loop i.e for/while etc with atime delay of 5 secs.

Regards,
Yogi
Reply With Quote
  #3 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 338
Stumble this Post!
Thumbs up

Thanks! I had used a infinte while loop, but was trying to avoid it. I would use this method.

Thanks a lot!!
Reply With Quote
  #4 (permalink)  
Old 03-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
Quote:
Originally Posted by nua7 View Post
I had used a infinte while loop, but was trying to avoid it.
Why? A program which is supposed to keep on running forever sounds like the basic example of when you might need an infinite loop.

You might want to have a "guardian" cron job to verify that the process is always running. In case of resource exhaustion or something, the OS might kill off long-running processes (especially the infamously aggressive "oom-killer" in recent versions of Linux).
Reply With Quote
  #5 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 338
Stumble this Post!
Thanks era! I am planning to put the script in crontab too!

Thanks all!
nua7
Reply With Quote
  #6 (permalink)  
Old 03-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
Also for a very long-running process, make sure it's not slowly hogging resources. Based on a brief test with Bash on Ubuntu Linux, the tail recursion suggested above will slowly eat up more and more memory. I'd definitely suggest an infinite loop instead.
Reply With Quote
  #7 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 338
Stumble this Post!
sure era.. This is something that I came up with (Sample code , since I cannot put in the actual code here..)

Please let me know if you have any suggestions in this.

Code:
#!/usr/bin/bash
#This is a sample watch dog script.
#This would test if script1 is running,if it is not running 
#it would start the script.

#function to start script1

function startscr1
{
sh /home/nua7/script1
print "Script 1 is starting"
exit 1
}

function chkscr1
{
cnt=`ps -ef | grep script1 | grep -v grep | wc -l`
if [[ $cnt -ne 1 ]]; then
    startscr1 
fi
}

wrapper=`ps -ef | grep wrapper | grep -v grep | wc -l`

if [[$wrapper -lt 1]];then
while true;do
chkscr1
done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0