Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top