Ending script after a specified time.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ending script after a specified time.
# 1  
Old 02-06-2006
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
# 2  
Old 02-06-2006
If i understand your requirements correctly, you can start the script in background and then sleep for a minute, then stop it.
# 3  
Old 02-06-2006
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

# 4  
Old 02-07-2006
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
# 5  
Old 02-07-2006
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>
# 6  
Old 02-07-2006
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

# 7  
Old 02-07-2006
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 Smilie
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check line end not ending with comma

I have several line in a text file. for example I like apple; I like apple I like orange; Output: I like apple I try to useif grep -q "!\;$"; then (Not work) Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. UNIX for Beginners Questions & Answers

School ending Script

Hello beautiful people, I would like to kindly ask you to help me with my school leaving project. Unfortunatelly due to family problems I felt into trouble and I am on able to finish my project due to schedule. I would like to ask you to review this script and give me any hints that you could.... (2 Replies)
Discussion started by: Cerda
2 Replies

3. Shell Programming and Scripting

Repetitive ending of script

Hi, I am quit satisfied with this scrtipt and really don't want to change anything but the end. I get a repetitve option when I want to "quit" and don't know why. What am I missing? When I press q to quit with this script, I get an EXTRA "Enter " How do I correct this so that when I press... (1 Reply)
Discussion started by: jefferj54
1 Replies

4. Shell Programming and Scripting

Ftp script hangs for first time,but works every second time

Hi I have an ftp script which works fine when i execute through a test scheduler(UC4), but when i run it through the prod scheduler(UC4), it hungs indefinetely, when we cancel the job and re-run it it works perfectly fine. here is the code,, any idea why this is happening ???? ... (1 Reply)
Discussion started by: selvankj
1 Replies

5. Shell Programming and Scripting

UNIX script abruptly ending due to ssh command

Below UNIX script abruptly ends while reading second line from file. When I comment 'ssh' command the script works as expected. I think I will have to run ssh command in a different process, but haven't got a handle yet as regards to how to do that. Any help in resolving this problem is highly... (1 Reply)
Discussion started by: jeeteshkc
1 Replies

6. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

7. Shell Programming and Scripting

sed in script creates output file ending with '?' (^M)

Hi, I'm trying to use sed within a shell script (bash, running ubuntu). The command works fine from the command line, but when I use it within the script, rather than creating a file with the name I've specified, it creates one that ends with a question mark '?' when you use ls, e.g.... (3 Replies)
Discussion started by: jennykay
3 Replies

8. Shell Programming and Scripting

Ending script at specific time.

Hello Everybody.. I've written the script that kick off through CRON job and kill itself by specific time. I've start time and end time specify in env file. i.e START_TIME=1500 (03:00 PM) END_TIME=0600 (06:00 AM) It always works good if my START_TIME is before midnight and my... (4 Replies)
Discussion started by: nirav_soni
4 Replies

9. Shell Programming and Scripting

Ending a script

Hi all, I am trying to end a Menu script. Can people suggest various methods please? At the moment I am doing: quit=n while do ...Code Code Code... read userinput case $userinput in q|Q) quit=y;; esac done But this doesn't seem to work every time, occasionally it will work,... (6 Replies)
Discussion started by: mikejreading
6 Replies

10. Shell Programming and Scripting

ssh not ending (sometimes) from inside a script, why?

Okay I have this script file that runs from cron and most the time it works just find. Except every so often one of the three ssh commands I have in it just doesn't know it's done and that of course causes the whole thing to hang! The ssh command has executed. I can tell this because the command... (1 Reply)
Discussion started by: stilllooking
1 Replies
Login or Register to Ask a Question