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
never ending loop ragha81 Shell Programming and Scripting 21 10-06-2006 03:07 PM
executables ending with * binums High Level Programming 2 01-25-2006 08:39 PM
File- Ending spikylina UNIX for Dummies Questions & Answers 3 03-08-2005 01:00 AM
ssh not ending (sometimes) from inside a script, why? stilllooking Shell Programming and Scripting 1 09-16-2004 04:42 AM
Ending Mail Peterh UNIX for Dummies Questions & Answers 2 05-11-2001 12:26 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-06-2006
Registered User
 

Join Date: Dec 2005
Posts: 33
Ending script after a specified time.

Hi All,

I am having a script, which run fine. But what I want to do is that the script should run for specified time and terminate then ( say for a minute).

Can somebody help me for this. I would be greatful.


Thanks,
Aru
Reply With Quote
Forum Sponsor
  #2  
Old 02-06-2006
Registered User
 

Join Date: Aug 2001
Posts: 179
If i understand your requirements correctly, you can start the script in background and then sleep for a minute, then stop it.
Reply With Quote
  #3  
Old 02-06-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
I guess you could put this in the start of your script...
Code:
echo "kill $$" | at now + 1 min
...but I can't really recommend it. You should consider rewriting your script. Presumably your script is using a loop to do something, so perhaps you could try something like this....
Code:
End=$((SECONDS+60))
while [[ $SECONDS -lt $End ]]
do
  : do something
done
Reply With Quote
  #4  
Old 02-06-2006
Registered User
 

Join Date: Dec 2005
Posts: 33
Hi,

Thanks for answering. Please confirm me that whatever I am writing below is what you asked me to do. In my script I want "top" to terminate after 1 min. As I am considerabely new to Unix. I have just written, what I am writing below in file x.sh.

Now when I run x.sh ....... it does not terminate after 1 minute. Please help me in modifying the below script.


>>>>>>>>>>>>>>>>>>>>>>>

End=$((SECONDS+60))
while [[ $SECONDS -lt $End ]]
do

top >> top.out
done

<<<<<<<<<<<<<<<<<<<<<<<<<<<<



Thanks,
Aru
Reply With Quote
  #5  
Old 02-07-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
if the checking is to be done inside the script itself,

as of,

the n seconds will not be guaranteed for the script to execute
where m seconds ( m < n ) would be taken for checking and other calculations,

rather,

have a wrapper script,
pid=call the process to be executed through the wrapper
do the checking (for certain amt of time in the wrapper script )
done
then pass the kill signal to process kill <val> <pid>
Reply With Quote
  #6  
Old 02-07-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
You never said that this was for the top command. Take a look at the manual pages for your version of top. You will be able to specify a delay and the number of iterations to run, e.g....
Code:
top -b -d 5 -n 12
Reply With Quote
  #7  
Old 02-07-2006
linuxpenguin's Avatar
Registered User
 

Join Date: May 2002
Location: India
Posts: 295
well in the above while loop i didnt see the SECONDS initiliased to 0, assuming it may not be required, there is no increment of the SECONDS in the while loop, so your while loop will surely not terminate
so in the while loop you may want to add a line
(( SECONDS = $SECONDS + 1 ))

i hope SECONDS is just a variable and no special shell built-in. Hence it has to be treated just like any other variable.
besides, you want SECONDS to really act as a SECOND, so in the while loop, put a sleep 1
__________________
War doesnt determine who is right, it determines who is left
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:17 PM.


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

Content Relevant URLs by vBSEO 3.2.0