tags:

views:

212

answers:

5

I have this bash script on the server that runs every hour, via cron. I was perfectly happy, but now the user wants to be able to configure the frequency through the web interface.

I don't feel comfortable manipulating the cron configuration programmatically, but I'm not sure if the other options are any better.

The way I see it, I can either:

  • Schedule a script to run once a minute and check if it should really run "now"
  • Forgo cron altogether and use a deamon that is its own scheduler. This probably means rewriting the script in python
  • ...or suck it up and manipulate the cron configuration from the web interface (written in python BTW)

What should I do?

EDIT: to clarify, the main reason I'm apprehensive about manipulating cron is because it's basically text manipulation with no validation, and if I mess it up, none of my other cron jobs will run.

Here's what I ended up doing:

Taking stefanw's advice, I added the following line at the top of my bash script:

if [ ! `cat /home/username/settings/run.freq` = $1 ]; then
    exit 0
fi

I set up the following cron jobs:

0 */2 * * *             /home/username/scripts/publish.sh 2_hours
@hourly                 /home/username/scripts/publish.sh 1_hour
*/30 * * * *            /home/username/scripts/publish.sh 30_minutes
*/10 * * * *            /home/username/scripts/publish.sh 10_minutes

From the web interface, I let the user choose between those four options, and based on what the user chose, I write the string 2_hours/1_hour/30_minutes/10_minutes into the file at /home/username/settings/run.freq.

I don't love it, but it seems like the best alternative.

+9  A: 

Give your users some reasonable choices like every minute, every 5 minutes, every half an hour, ... and translate these values to a cron job string. This is user friendly and forbids users to tamper directly with the cron job string.

stefanw
Sounds good, I hope client would be satisfied with it.
Clement Herreman
A: 

What about Webmin? I have never used it myself but it seems you could configure the cron module and give permissions to the user who wants to configure the job.

WMR
That's quite an overkill, I'm afraid
itsadok
Yes, it's a big solution for a small problem. It might still be faster to set up and easier to maintain than anything "homegrown". This depends heavily on your current environment of course.
WMR
+3  A: 

You could use a python scheduler library that does most of the work already:

ars
A: 

Well something I use is a main script started every minute by cron. this script check touched files. If the files are there the main cron script start a function/subscript. You just have to touch a defined file and "rm -f"ed it when done. It has the side benefits to be more concurrent proof if you want other way to start jobs. Then you can use your favourite web programming language to handle your users scheduling ...

The main script looks like :

[...]

if [ -e "${tag_archive_log_files}" ]; then
    archive_log_files ${params}
    rm -f ${tag_archive_files}
fi

if [ -e "${tag_purge_log_files}" ]; then
    purge_log_files ${params}
    rm -f ${tag_purge_log_files}
fi

[...]
neuro
But now I still have to create those tag files on a schedule, don't I?
itsadok
Yes. but now you don't have to manipulate cron files nor do complex things with your language of choice. You just have to create/delete files. You decorrelate the scheduler jobs from the application.
neuro
A: 

I found a module that can manipulate the cron info for me. It's called python-crontab, and it's available with easy_install. From the source:

Example Use:

from crontab import CronTab

tab = CronTab()
cron = tab.new(command='/usr/bin/echo')

cron.minute().during(5,50).every(5)
cron.hour().every(4)

cron2 = tab.new(command='/foo/bar',comment='SomeID')
cron2.every_reboot()

list = tab.find('bar')
cron3 = list[0]
cron3.clear()
cron3.minute().every(1)

print unicode(tab.render())

for cron4 in tab.find('echo'):
    print cron4

for cron5 in tab:
    print cron5

tab.remove_all('echo')

t.write()

(I kept googling for "cron" and couldn't find anything. The keyword I was missing was "crontab").

I'm now deciding if I'll use this or just replace cron entirely with a python based scheduler.

itsadok