主要使用的是以下扩展[jobby](https://github.com/jobbyphp/jobby) #### 1.安装扩展 ~~~ composer require hellogerard/jobby ~~~ #### 2.添加定时任务 ~~~ * * * * * cd /path/to/project && php jobby.php 1>> /dev/null 2>&1 ~~~ #### 3.部署文件 ~~~ cp vendor/hellogerard/jobby/resources/jobby.php ./ ~~~ ## example ~~~ <?php //https://github.com/jobbyphp/jobby github,地址 //http://www.xiabin.me/2016/06/16/php-note9/ 说明 require_once 'vendor/autoload.php'; $jobby = new \Jobby\Jobby(); //额外的配置 $data = [ // 'recipients' => '465497241@qq.com', // 'mailer' => 'stmp', // 'smtpHost' => 'smtp.mxhichina.com', // 'smtpPort' => '587', // 'smtpUsername' => 'system@cfo2o.com', // 'smtpPassword' => 'SSy123456', // 'smtpSender' => 'system@cfo2o.com', // 'smtpSenderName' => 'Crontab', // 'debug' => config('APP_DEBUG') ]; /** * @Author: 陈静 * @Date: 2018/05/14 22:40:19 * @Description: 自动分成,每两个小时执行一次 */ $jobby->add('Distribute', [ 'command' => 'cd /home/www/dev_tpshop && /usr/local/php7.1/bin/php think distribute', 'schedule' => '* */2 * * *', 'enabled' => true, 'output' => 'runtime/log/'.date('Ym').'/'.date('d').'_distribute.log' ]); /** * @Author: 陈静 * @Date: 2018/05/14 22:49:26 * @Description: 计算用户kpi,每周日0点10执行 */ $jobby->add('Calculate', [ 'command' => 'cd /home/www/dev_tpshop && /usr/local/php7.1/bin/php think calculate_kpi', 'schedule' => '10 0 * * 0', 'enabled' => true, 'output' => 'runtime/log/'.date('Ym').'/'.date('d').'_distribute.log' ]); $jobby->run(); ~~~