*/30 */30 * * * python /root/get_top.py
I'm trying to run a script every 30 hours and 30 minutes. Is the syntax above proper for that?
I've had multiple people tell me that */30 is not a valid value for the hour column, since it's greater than 24. If that's true, how do I make a cron for a job that should run every 30 hours?
Best Answer
The simplest solution would probably be to run a cronjob more frequently and use a wrapper script to quit without doing anything if not enough time has passed.
To figure out how often you need to run, take the greatest common factor of cron's limits and your desired interval.
So, for "every 30 hours, 30 minutes", that'd be "every 30 minutes" and, for "every 30 hours", that'd be "every 6 hours" (The greatest common factor of 30 and 24)
You can implement the wrapper one of two ways:
First, you could store a timestamp in a file and then check if the time difference between now and the stored timestamp is greater than or equal to 30 hours and 30 minutes.
This seems simple enough, but has two potential gotchas that complicate the code:
The second option is to not store a timestamp file at all and, instead, do some math. This is also theoretically faster since the kernel can return the system time without querying the hard drive.
I haven't tested this for typos, but here's Python code for it that's been expanded out for clarity.
Here's the idea behind it:
minutes_since_epoch % full_interval
will only be less thancron_interval
once perfull_interval
.[0, cron_interval)
as a window within which a task must fall in order to be executed.time.time()
.If, as I suspect,
get_top.py
is your own creation, just stick this at the top of it and change the check to