PHP code example of peppeocchi / php-cron-scheduler
1. Go to this page and download the library: Download peppeocchi/php-cron-scheduler library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
peppeocchi / php-cron-scheduler example snippets
GO\Scheduler;
// Create a new scheduler
$scheduler = new Scheduler();
// ... configure the scheduled jobs (see below) ...
// Let the scheduler execute jobs which are due.
$scheduler->run();
$scheduler->php('path/to/my/script.php');
$scheduler->php(
'path/to/my/script.php', // The script to execute
'path/to/my/custom/bin/php', // The PHP bin
[
'-c' => 'ignore',
'--merge' => null,
],
'myCustomIdentifier'
);
$scheduler->php('script.php')->output([
'my_file1.log', 'my_file2.log'
]);
// The scheduler catches both stdout and function return and send
// those values to the output file
$scheduler->call(function () {
echo "Hello";
return " world!";
})->output('my_file.log');
$scheduler->php('script.php')->output([
// If you specify multiple files, both will be attached to the email
'my_file1.log', 'my_file2.log'
])->email([
'[email protected]' => 'My custom name',
'[email protected]'
]);
$scheduler->php('script.php')->when(function () {
// The job will run (if due) only when
// this function returns true
return true;
});
$scheduler = new Scheduler([
'tempDir' => 'path/to/my/tmp/dir'
]);
$scheduler->php('script.php')->onlyOne();
$scheduler = new Scheduler();
// This will use the default directory path
$scheduler->php('script.php')->onlyOne();
$scheduler->php('script.php')->onlyOne('path/to/my/tmp/dir');
$scheduler->php('other_script.php')->onlyOne('path/to/my/other/tmp/dir');
// $logger here is your own implementation
$scheduler->php('script.php')->before(function () use ($logger) {
$logger->info("script.php started at " . time());
});
// $logger and $messenger here are your own implementation
$scheduler->php('script.php')->then(function ($output) use ($logger, $messenger) {
$logger->info($output);
$messenger->ping('myurl.com', $output);
});
$scheduler->php('script.php')->then(function ($output) use ($logger) {
$logger->info('Job executed!');
}, true);
// $logger here is your own implementation
$scheduler->php('script.php')
->before(function () use ($logger) {
$logger->info("script.php started at " . time());
})
->then(function ($output) use ($logger) {
$logger->info("script.php completed at " . time(), [
'output' => $output,
]);
});
# some code
$scheduler->run();
# ...
// Reset the scheduler after a previous run
$scheduler->resetRun()
->run(); // now we can run it again
// get all failed jobs and select first
$failedJob = $scheduler->getFailedJobs()[0];
// exception that occurred during job
$exception = $failedJob->getException();
// job that failed
$job = $failedJob->getJob();
$scheduler = new Scheduler();
$scheduler->php('some/script.php');
$scheduler->work();