How to make a script run for a maximum of "x" number of hours only

How to make a script run for a maximum of "x" number of hours only

What do you want actually.

  1. Run a script for specific time stamps ( you can do through crontab)
  2. Run a script which executes hourly basis ( implement with "for" and Sleep)

Can you evaluate your requirement pls..?

Hi,

Basically I want the script to run not more than 5hrs. This script actually downloads files from a server. So I want to have a timecheck so that its not more than 5hrs the script is running

I am using the following function in one of my scripts:

sleep_wake_kill() {
        sleep 21600
        kill $(ptree $$ | awk '{if(NR>1) print $1 }')
}

I call this at the beginning of my code and put it in the background. The function will sleep for six hours, then wake up and kill everything that has anything to do with this process (including the login session).

Call it like this:

#!/usr/bin/ksh
### assuming that sleep_wake_kill function is already defined
sleep_wake_kill &
# rest of the code follows
...

-Edit
If anyone has another idea, I would love to hear it. I don't know if what I am doing is the best way to handle this.

I guess you could try...

run_script.sh &
echo "kill $!" | at now + 5 hours

...assuming that you don't cycle through all available process numbers every few hours.

It is probably safer to create a run file with the pid at the script's beginning and then remove it at the end. Then impliment a for loop as stated before, but instead of killing everything with the specified PID you could check for the run file and if that exists then kill the pid specified in that file.

if you could give some examples it would be great

Hi,
I want to load the files in a hourly base for an entire Day. can you please suggest how to load. Actually iam having 5 Process_environements, i want to use these 5 Environements.

1 Hr : Pr1
2 Hr : Pr2
3 Hr : Pr3
4 Hr : Pr4
5 Hr : Pr5
6 Hr : Pr1
7 Hr : Pr2
.
.
.
.
11Hr : Pr1
.
.
.