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?

3 Answers 3

8

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.

1
  • 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
    Nov 29, 2009 at 15:18
2

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.

1

Actually, you can manipulate the cron jobs as simple as text files. You have to be careful not to brake anything tho.

The cron daemon saves the cronjobs for each user using the following structure:

/var
   /spool
        /cron
            /crontabs
                /root
                /user1
                /user2
                ..
                /user5

Using this information, you can modify the text file corresponding the user for which you would like to set the new cron.

Be aware that modifying system files can be dangerous and you should be extremely careful. Your application will also need some additional privileges for modifying these files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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