I have a site on my webhotel I would like to run some scheduled tasks on. What methods of achieving this would you recommend?
What I’ve thought out so far is having
I would outsource the cronjobs with www.guardiano.pm and call a url every X minute. When your url (i.e www.yoursite.com/dothis.php) is called than you execute some code. If you don't want to let the web request the page when you want you can allow only request in POST and send some parameter that only you know with guardiano.pm
Thats what I would do because I do that on my pet projects. Have fun!
If you do not have the option to setup a cronjob you can call the script with cUrl (as alternative to wget - same functionality). Just do a scheduled task on your local machine that executes the cUrl function.
Command line PHP + cron would be the way I would go. It's simple and should fit the bill. It is usually installed with PHP as a matter of course.
if you're wondering how to actually run your PHP script from cron, there are two options: Call the PHP interpreter directly (i.e., "php /foo/myscript.php"), or use lynx (lynx http://mywebsite.com/myscript.php). Which one you choose depends mostly on how your script needs its environment configured - the paths and file access permissions will be different depending on whether you call it through the shell or the web browser. I'd recommend using lynx.
One side effect is that you get an e-mail every time it runs. To get around this, I make my cron PHP scripts output nothing (and it has to be nothing, not even whitespace) if they complete successfully, and an error message if they fail. I then call them using a small PHP script from cron. This way, I only get an e-mail if it fails. This is basically the same as the lynx method, except my shell script makes the HTTP request and not lynx.
Call this script "docron" or something (remember to chmod +x), and then use the command in your crontab: "docron http://mydomain.com/myscript.php". It e-mails you the output of the page as an HTML e-mail, if the page returns something.
#!/usr/bin/php
<?php
$h = @file_get_contents($_SERVER['argv'][1]);
if ($h === false)
{
$h = "<b>Failed to open file</b>: " . $_SERVER['argv'][1];
}
if ($h != '')
{
@mail("cron@mydomain.com", $_SERVER['argv']['1'], $h, "From: cron@mydomain.com\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1");
}
?>
Have you ever looked ATrigger? The PHP library is also available to start creating scheduled tasks without any overhead.
Disclaimer: I'm among their team.
The method you are using is fine, if you don't want to use cronjobs or anything external, but these can be heavy to check each time a page loads.
At first, some cronjobs can probably be replaced. For example if you have a counter for how many users have registered on your website, you can simply update this number when a user registers, so you don't have to use a cronjob or any scheduled task for this.
If you want to use scheduled tasks, I suggest you to use the method you are using right now, but with a little modification. If you're site has enough hits on a day, you can simply make the tasks run (or the tasks check function run) only for 1% or maybe 0.01% of the hits instead of all of them, the percentage you should use depends on the page hits you have and how many times you want to run the task. So, simply add a randomizer to achieve this feature.
You could simply use a function like this;
if(rand (1, 100) <= 1) { // 1, 100 is used to generate a number between 1 and 100. 1 is for one percent.
// Run the tasks system
}