I want to programatically change the hour which certain cron jobs execute. I'm not sure whether I'm going about this the right way, but here's the plan.
Jobs which are subject to change will have a comment on the end of the line "#change-enabled".
15 5 7,14,21,28 * * /path/to/executable1 #change-enabled
15 * * * * /path/to/executable2
45 5 */2 * * /path/to/executable3 #change-enabled
Then I want to pipe the output from "crontab -l" through sed to catch and alter jobs with that comment on the end, and pipe the results of that through "crontab -".
// Edit
My gut tells me to stay away from doing this as root because the new hour is determined using data from a 3rd party. (Google Analytics Data Export API) So /etc/cron.d/ is something I'm going to stay away from for this application.
Here is the command which has been working with all of my current cron jobs during debugging. I've been using "date +%l" in place of "/home/user/slow-hour" and leaving off the "| cron -" to see what my gets printed.
crontab -l | sed -e "s/^\([^ ]*\) [0-9]* \(.*#change-enabled\)/\1`/home/user/slow-hour` \2/" | crontab -
The contents of ~/slow-hour will be a script which fetches hourly data for website profiles at Google Analytics and looks for the slowest hour. In the event of any error whatsoever that script will return 1AM, mail me a notice and then bail.
In order the reduce the likelihood of a days execution being skipped due to the slow hour being earlier than the current hour, the automated cron rescheduler will be set to run at midnight and the earliest hour ~/slow-hour will return will be 1AM.
Based on my manual cron job adjustments in the past, I don't anticipate excluding midnight from being the slowest hour being a problem in my case.