tags:

views:

89

answers:

2

I want to have a web interface to control when to call a PHP script. In this interface I want to be able to set certain times or ranges that a script will be called. This information will be stored in a DB. When I hit save I want to have it read all the scheduled times from the DB, then format it and write it to a crontab for a cron job. Is there a way to have PHP modify a crontab file?

+2  A: 

It should be possible, but you need PHP to have the necessary rights to access the user's crontab or to execute "crontab" from the command line. In a shared hosting environment this is mostly not the case.

Pekka
+7  A: 

I would go for creating a "cron_runner.php" script that runs every minute, reads the cron data and runs them accordingly. It feels like reinventing the wheel but at least you have no problems with the file permissions.

rpSetzer
An alternative might be to have multiple "cron_runner" files; one set to execute evety minute, one running every 15 minutes, one every hour, etc. That way, you don't need as much logic to decide which scripts to run, and there's less risk of your one-minute cron file taking more than 60 seconds to execute. Only useful if you have a LOT of scripts to be run, though. Plus, it limits you to choosing one of the available timers, instead of setting your own.
Duroth