Log in

View Full Version : PHP controlling crons?



MrCraig
15-09-2008, 03:17 PM
Heyyy :),
Does anyone know how i can get PHP to run a cron job that will execute a script on the first of every month?

NN-Dom
15-09-2008, 03:22 PM
create a file called crons.txt
put the WHOLE cron (including the * * * * * stuff, etc) you want to run it in
and in the same directory, create a php file that has the following contents in it:


<?php exec("crontab /directory/to/crons.txt");?>then save + go to the file in you browser, then delete the file :)

MrCraig
15-09-2008, 05:01 PM
so i could run that on installation?

And what code do i need to run a php file (in the crons.txt thing)

MrCraig
15-09-2008, 06:09 PM
Also how would i go about editing existing crons that are set up?

Dentafrice
15-09-2008, 07:57 PM
Not all systems allow the use of crontab or exec() in general.

It depends on the user that Apache/cPanel is runinng on, and what permissions they have, if they have permission to edit the crontab from PHP/that user.

MrCraig
15-09-2008, 08:29 PM
Is there a way of finding out if a site is allowed use of crontab ? Like i could do something where if their not allowed, the option to enable the cron services is disabled or something like that?

Dentafrice
15-09-2008, 08:31 PM
you *might* could do something like this, not sure if exec returns true/false though.



<?php
if(exec( "blabla" ) == false) {
exit( "ERROR! Could not access crontab. Please do this step manually." );
}
?>

MrCraig
16-09-2008, 03:01 PM
Return Values

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

To get the output of the executed command, be sure to set and use the output parameter.


Apparently not ;p

hm it might just be easier to use a system LIKE pseudocron instead..
More control and cross-server compatability that way.

What you guys think?

RYANNNNN
16-09-2008, 03:33 PM
Add the cron in cpanel cron manager.

IIRC you need:
php -f /path/to/script.php

MrCraig
16-09-2008, 04:11 PM
Il just use a pseudocron like method ;p

And ryan - if i were to use crons it would have to be controlled by the script :P
Thanks anyways guys :)

Want to hide these adverts? Register an account for free!