I'm developing a web application that requires the use of Cron. I'd like to make it easy to setup with an auto install process like Wordpress. I have no problems writing the install script up en till its time to set up Cron. Please tell me if I can do this.
Asked
Active
Viewed 9,769 times
1 Answers
9
You just have to create the cron file, then use exec to set up that cron:
$cron_file = 'cron_filename';
// Create the file
touch($cron_file);
// Make it writable
chmod($cron_file, 0777);
// Save the cron
file_put_contents($cron_file, '* * * * * your_command');
// Install the cron
exec('crontab cron_file');
This requires that the user which PHP is run under has the right to make crontabs. This cron file will by default replace any other crons for that user, so make sure to ask the user if he wants to apply the cron. Also make sure the folder you're writing the crontab file in is writable.

Tatu Ulmanen
- 123,288
- 34
- 187
- 185
-
1Don't use `chmod 0777`. The rights should be limited to the minimum, in this case `0600`. And, as already said, the PHP interpreter must be allowed to edit the crontab and even that is the case, it can conflict with other cronjobs from the same user. – deamon Jan 10 '10 at 14:05
-
so if CronTab is not accessible to Apache its no go right? This is what I'm afraid of. – Robert Hurst Jan 10 '10 at 14:11
-
2Is there a way to do this and keep the existing cron jobs running? Someway to append a new cron job? Also, a way to delete a specific cron job? – DCR Apr 13 '17 at 13:14