Running a process based on time

Hello All,

My script is nearly complete, there is just one last piece that needs to be added in.

I need to check for the time, and if it is lets say for example. Sunday at 5:00AM, my script cannot run.

I would assume it would be something like this, parden the terrible pseudocode

check the time
if the time is sunday between 5am and 8am
Do nothing
if else
do the regular scripting.

whats the best way to get started doing this?

Well, if you want downtime, make a separate marker file with a state indicator, so you can turn it off unconditionally or allow it to run. Then you can have cron jobs change the state, or you can take it down during special maintenance.

What asks this script to run?

Now=$(date +%u%H) # %u: 1..7 1 is Monday, %H: Hour 00..23
if [ $Now -lt 705 ] || [ $Now -gt 708 ] # 705: sunday 5 AM, 708 ... 8 AM
then 
   # do regular scripting
fi

Not sure if your script need be run regularly. Do you need the backup between 5am ~ 8am? If it is, to put it in cronjob will be good idea, with that you needn't change your code if the time is changed in the future. For example, the backup time is changed.

0 0-4,9-23 * * 0 /PATH/your_script

How can $Now be both less than 705 and greater than 708?

rdcwayx, it sounds like this script is a start/login script that probably runs from .profile. If this is the case, scheduling it to run via cron is unlikley to be usefull.

I think DGPickett is on the right path have your backup (or whatever else is running at 5am Sunday) create a lockfile and check it with the start/login script, that way you can also stop it running for other ad-hoc maintence, or whatever.

:eek: I forgot to change the AND in OR when inverting the condition !
I Corrected the original post.

Yes, this is the toe of the "I wish I had a scheduler" problem, where you need to control and track many things running periodically, more elaborately than cron, be able to layer schedules, stop scheduling, run later or rerun tasks that can be focused on their original time(s) and restart running tasks that are truly real time, possibly with an immediate run off-cycle to minimize the gap. For instance, disk cleanup tasks are in the latter group (real time), and shift reports are in the former group (can have a date and shift as arguments, make aggregate rows in a db for one shift). If the maintenance takes 10 hours not 8, you do not want things taking off after 8, as the cleanup may take hours. Then, there is the possibbility of creating job dependencies, so b for today does not run until a for today and b for yesterday run OK. You can even have it mark dependent jobs not run if you mark a run as defective, automating catch up reruns. Optimally, it also provides you with a database of run times and outlooks that can drive scheduling choices and resource management as well as reruns, perhaps after a problem is fixed, and even load factors to drive smart scheduling.

crontab is not an option..