PHP code example of smichaelsen / php-cron-scheduler
1. Go to this page and download the library: Download smichaelsen/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/ */
GO\Scheduler;
function myFunc() {
return "Hello world from function!";
}
$scheduler = new Scheduler([
'emailFrom' => '[email protected]'
]);
/**
* Schedule cronjob.php to run every minute
*
*/
$scheduler->php(__DIR__.'/cronjob.php')->at('* * * * *')->output(__DIR__.'/cronjob.log');
/**
* Schedule a php job to run with your bin
*
*/
$scheduler->php(__DIR__.'/cronjob.php')->useBin('/usr/bin/php')->at('* * * * *')->output(__DIR__.'/cronjob_bin.log', true);
/**
* Schedule a raw command to tun every minute between 00 and 04 of every hour,
* send the output to raw.log
* Pass `true` as a second parameter to append the output to that file
*
*/
$scheduler->raw('ps aux | grep httpd')->at('* * * * *')->output(__DIR__.'/raw.log', true);
/**
* Run your own function every day at 10:30
*
*/
$scheduler->call('myFunc')->every()->day('10:30')->output(__DIR__.'/call.log');
$scheduler->call(function () {
return "This works the same way";
})->at('* * * * *')->output(__DIR__.'/call.log');
/**
* Run only when your func returns true
*
*/
$scheduler->php(__DIR__.'/cronjob.php')
->at('* * * * *')
->when(function () {
return false;
})->output(__DIR__.'/never_created.log');
/**
* Send the output to an email address
*
*/
$scheduler->call(function () {
return "This will be sent via email";
})->at('* * * * *')->output(__DIR__.'/call.log')->email('[email protected]');
$scheduler->run();