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 END_TIME is after midnight.
I'm using follwing if condition to terminate my script.
if [ $CURRENT_TIME -gt $START_TIME ] || [ $CURRENT_TIME -lt $END_TIME ];
But if I wanted to change my START_TIME and END_TIME to like
START_TIME=1300 (01:00 PM)
END_TIME=1530 (03:30 PM)
then it's not follow the logic and script will run forever.
When job kick off through CRON then script will check that kick off time is after the START_TIME and before the END_TIME.
gives the current time in terms of seconds since 1970. So you can terminate your script in a spesific time. 1 hours is 3600 seconds so lets say to finish your script after 2 hours you have to calculate the current time again and compare it with (now+7200)
i just wanted to give an idea
regards
---------- Post updated at 11:11 PM ---------- Previous update was at 11:10 PM ----------
Those two are more concise, and accomplish the same thing ( return the seconds since the epoch 00:00:00 Jan 1, 1970 GMT, not counting leap seconds ).
The date command is GNU date 7.2 on Linux. Use Perl, if you are worried about portability of /bin/date
Here is a simple example of a perl script to do what you want:
my $begin_time = time;
my $offset = 10; # number of seconds
do {
print "foo\n";
sleep 1;
} while ( time < ($begin_time + $offset) );
this grabs the current epoch time and then executes the code in the do() block ( in this case, prints "foo" once every second ). It keeps doing this for 10 seconds total.
What you need to do is put the start time in as a field in your crontab which runs this job, and then calculate the offset in seconds from that time.
In the example you gave this is 60 seconds * 60 minutes * 15 hrs, or 54,000 seconds. that is what you should set $offset to.
I would actually set it, not "54,000" but the equation, as it helps you to see what is going on:
my $offsest = 60 * 60 * 15; # easier to understand this is 15 hours
Some versions of date don't have the %s (Solaris is one) modifier. Not all UNIX flavors have truss. I would recommend using the perl method deindorfer posted.
Thanks for the reply deindorfer!!!
I did understood your logic and it'll work if i only have to control the end time.
I'm sorry I mention CRON actually this job run through CA7 scheduler which is not in my control. CA7 will always kick off the job at specific time but as requirement I�ve to check in my script that kick off time is after the START_TIME.
As you said that convert the END_TIME in second (i.e. 60 * 60 * 15 = 54000 sec)
that means my job will run for 54000 seconds.
For example in my environment file I�ve
START_TIME=2100
END_TIME=0600
If I convert my END_TIME in seconds then it'll only run for 6 hours.
The user will put actual time to end the script they won't put the total hours to run the script.
I guess I may need to get the difference between START_TIME and END_TIME to get the total hours and then run the script for that total hours.
But I�m still stuck with that START_TIME comparison.