Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a hosting on hostgator and I just found out that they don't allow * * * * * cron jobs. If I have one that runs less than every 15 minutes, they change it to */17 or something like that. I searched but I can't find a way to do this on my own installations of cPanel. Any hints on how to replicate this.

Note: I'm not looking for an answer to by-pass this restriction. I'm trying to find out how to implement it on my own deployments.

Thanks.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

I'm not aware of a standard package implementing this feature.

You can, of course, create a root cron job that checks and "fixes" user cronjobs. Take the following script for example - it's untested mostly, but it worked with the couple of examples I threw at it.

#!/bin/bash
min=16 # the minimum allowed interval between runs
for crontab in /var/spool/cron/crontabs/*
do
  cp $crontab $crontab.bak
  # replace the * * * * * jobs with */16 * * * * jobs.
  sed -i "s/\* \* \* \* \*/\*\/16 * * * */" $crontab
  for i in `seq 1 $min`
  do  
    # replace */N * * * * for every N <= min
    sed -i "s/\*\/$i \* \* \* \*/*\/16 * * * */" $crontab
  done
done

This assumes the crontab fields are separated by spaces, which may not be true - I used that assumption to keep the regex (hopefully) understandable. To make it more robust replace all spaces within the left-hand regexes with \s, if your sed supports it, or [ \t]+ (I think) if it doesn't.

Do note that the example applies 17 different search-and-replaces over each crontab, which of course will probably be unacceptable when the number of crontabs increases. To alleviate this issue you can use a single regex to match all possible infringing cronjobs. For example something like (untested): \*(/[0-9][0-5])* \* \* \* \* or a procedurally constructed one using the output of seq -s '|' 0 $min. Let me know if you need help with that.

share|improve this answer
    
Awesome. Thanks for the script. I'll wait a little while to see if someone else can point out a better solution (for example with a WHM addon). –  recluze Jun 13 '11 at 3:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.