1、生成任务类 App\Console\Commands\Test.php
php artisan make:command SendEmail
2、打开文件 App\Console\Commands\Test.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendEmail extends Command
{
protected $signature = 'email:send';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
Email::content('测试邮件')->send('666@abc.com');
}
}
3、查看命令列表
php artisan list
4、在App\Console\Kernel.php 注册并设置运行规律
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
\App\Console\Commands\test::class, //注册命令
];
protected function schedule(Schedule $schedule)
{
$schedule->command('email:send')->everyMinute(); //设置运行时间间隔
// $schedule->command('inspire')->hourly();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
5、将命令加入到 linux命令中去。
crontab -e
写入以下内容(项目的绝对路径)
* * * * * php /www/wwwroot/wx.guolubao.com/artisan schedule:run >> /dev/null 2>&1
附
->cron('* * * * *'); 在自定义Cron调度上运行任务
->everyMinute(); 每分钟运行一次任务
->everyFiveMinutes(); 每五分钟运行一次任务
->everyTenMinutes(); 每十分钟运行一次任务
->everyThirtyMinutes(); 每三十分钟运行一次任务
->hourly(); 每小时运行一次任务
->daily(); 每天凌晨零点运行任务
->dailyAt('13:00'); 每天13:00运行任务
->twiceDaily(1, 13); 每天1:00 & 13:00运行任务
->weekly(); 每周运行一次任务
->monthly(); 每月运行一次任务