AutoCrontabJob
, (*1)
An object-oriented approach to scheduling regular cron jobs automatically, (*2)
Install
Include in your composer.json
:, (*3)
"require": {
"battis/autocrontabjob": "~1.0"
}
Use
This object requires a bit of prior preparation before use:, (*4)
- Extend the abstract
Battis\AutoCrontabJob
class and implement the abstract scheduledJob()
method.
- Create a script that will be run regularly as a Cron job, that instantiates your class and calls its
scheduledJob()
method.
Gotcha warning: Remember that, when a script is run by cron, it is not run by Apache, so it will not generate output to the Apache log files. Handily, there is a log built-in that you can use (see below). By default, the log will be generated in the same directory as the script and be similarly named (.log
instead of .php
)., (*5)
A sample script file (which is an all-in-one, also extending the abstract class), called MyJob.php
:, (*6)
log->log('I did something!');
}
}
// instantiate our class
$job = new MyJob(
'example',
__FILE__, // *this* file will be called by crontab
'*/5 * * * *' // run every five minutes (woo hoo!)
);
// fire the scheduledJob() method
$job->scheduledJob();
?>