tags:

views:

573

answers:

5

Here's my situation: We have an outside provider that will be pushing XML files to our server every 30 seconds at a specified time in the week.  That time will vary each week. So, I would like to give my users an interface on the management side of our site where they could "push a button" and trigger the capturing of data. (and in the same way be able to turn the job off)

I have planned to do this using PHP. I assume that I'll need to run some system commands from my script to schedule and delete cron jobs. 

My question is: Is this the best way to do this? Is there any danger in scheduling and deleting cron jobs from php?  What things do I need to keep in mind as I'm creating this?

Also, any pointers/hints at how to accomplish this are more than welcome. Thanks in advance. 

+3  A: 

I would recommend just keeping the cron job that looks for the uploaded XML files running all the time, every minute. Checking to see if there are any files in a directory is not an expensive enough operation that you need to make an involved administrative interface to turn it off when not needed.

chaos
I should have made this part of my question... I think I would need to have the job run every 30 seconds or so to pull in the most recent data. Will parsing a reasonably sized xml file possibly every 30 seconds have a negative impact on performance? Or is that a moot point?
Anthony
If it's 'reasonably sized' I doubt you're going to hurt anything. But note that the finest resolution that `cron` has is once every minute; if you must have processing every 30 seconds, you'll need to have the cron job sleep for 30 seconds and do a second check, or just make a daemon.
chaos
Ah, I forgot that cron only goes down to the minute and not the second. Thanks for the help.
Anthony
+1  A: 

You could read in the crontab file and add/uncomment or comment-out the line that runs your script. That would make me nervous, because an error could leave your crontab broken, affecting other jobs.

You can also just leave the cron job running. Have your on/off switch set a value in some other file or database, then have the cronned script check that value to determine whether it should actually do anything or just exit.

Or, just leave the cron job running all the time so your users always have fresh data and don't have to remember to turn on file reading.

Or, set up the cron job so it only runs at that particular time of the week.

Scott Saunders
+2  A: 

I would suggest that you do not add or delete cron jobs using PHP. You should write a (fixed) cron job instead that would be configured using PHP instead. It could check some config file or database each time it runs for actual jobs that you queued.

cg
A: 

CG has more or less what I do. Instead of having everything in a bash script, I simply have a script which reads:

php cron.php;
exit 0

This lets me use PHP to do everything. Granted, this is not as efficient as using Bash, but it has the benefit of making it so that I can write in a language I know a lot better.

Christopher W. Allen-Poole
A: 

I've done this sort of thing before. I don't recommend turning the cron on/off at the crontab level.

Instead, leave the cron running all the time, but the PHP script should look like:

if( ScriptIsEnabled() )
{
  DoWork()
}

ScriptIsEnabled() should look for a flag in a config file, database, memcache, or how ever you want to communicate to the script. I prefer memcache (least overhead -- just set a value) over a database.

You can then have a web gui with an on/off button to control this.

Gary Richardson
+1 This is a very good and most reliable way to do it especially if you can't manipulate the crontab directly because of security privileges.
Helen Neely