Friday, January 28, 2011

Is it possible to configure cron (on linux) to read a user crontab from a file checked in to an application?

Hi,

I'd like a set of hourly, daily, weekly files like in /etc/ but for each application I run.

I'd ideally like cron to look in

/srv/www/*/current/config/cron for them.

So that I can add a new Rails app to /srv/www and have cron pick up the new jobs the next time it runs.

Is this wishful thinking?

Thanks,

-Tim

  • Have cron execute a helper program hourly, daily, etc and have this program determine whether there is work to be done

    From adamo
  • A typical crontab will have entries such as

    # run-parts
    01 * * * * root run-parts /etc/cron.hourly > /dev/null 2>&1
    02 4 * * * root run-parts /etc/cron.daily > /dev/null 2>&1
    22 4 * * 0 root run-parts /etc/cron.weekly > /dev/null 2>&1
    42 4 1 * * root run-parts /etc/cron.monthly > /dev/null 2>&1
    

    I'm no expert but can't see any reason you can't add other directories in there in the same way. If you want it to be more automatic you could have a script run periodically to look for cron.* directories starting at your chosen root and programatically add those paths to /etc/crontab if they don't already exists there.

  • First, create a Bash script like so:

    #!/bin/bash
    for web_job in /srv/www/*/current/config/cron/*;
     do [ -x $web_job  ] && $web_job || : ;
    done
    

    Save it as something like /srv/www/run_web_jobs.sh. Then, add it as a cron job. For example, for it to run daily (must be root):

    $ ln -s /etc/cron.daily/ /srv/www/run_web_jobs.sh
    

    Or, add it to a user's crontab.

    $ crontab -e
    
  • Are you trying to have the equivalent of cron.hourly, cron.daily, cron.weekly, and cron.monthly directories with scripts in them for each rails app? If so, building off of what John said, if you were to add the following to your /etc/crontab, you'd get the desired result:

    01 * * * * root for i in /srv/www/*/current/config/cron.hourly ; do run-parts "$i" > /dev/null 2>&1 ; done
    02 4 * * * root for i in /srv/www/*/current/config/cron.daily ; do run-parts "$i" > /dev/null 2>&1 ; done
    22 4 * * 0 root for i in /srv/www/*/current/config/cron.weekly ; do run-parts "$i" > /dev/null 2>&1 ; done
    42 4 1 * * root for i in /srv/www/*/current/config/cron.monthly ; do run-parts "$i" > /dev/null 2>&1 ; done
    

    Each application can then just specify the subset of cron.X directories it needs and fill it with the relevant scripts.

    However if you want to have just an optional script for each frequency for each application, then you'd want to do something more like:

    01 * * * * root for i in /srv/www/*/current/config/cron.hourly ; do [ -x "$i" ] && "$i" ; done
    02 4 * * * root for i in /srv/www/*/current/config/cron.daily ; do [ -x "$i" ] && "$i" ; done
    22 4 * * 0 root for i in /srv/www/*/current/config/cron.weekly ; do [ -x "$i" ] && "$i" ; done
    42 4 1 * * root for i in /srv/www/*/current/config/cron.monthly ; do [ -x "$i" ] && "$i" ; done
    

    Again, this would only run for applications that specified them.

    One of these what you're looking for?

    John Gardeniers : +1 I like the elegance and it's a whole lot simpler than what I suggested.
    From mark

0 comments:

Post a Comment