Console 定时脚本
创建脚本
app\Console\Commands 目录下创建脚本文件 TestConsole.php
php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Base\CommonPoolMethod;
//连接数据库可引入数据库相关类
class TestConsole extends Command
{
use CommonPoolMethod;
/**
* The name and signature of the console command.
* update:artisan下命令组,可使用【php artisan -v】或【php artisan list】查看
* test_function :解释命令作用(随意)
* 完整命令【php artisan update:test_function】
* @var string
*/
protected $signature = 'update:test_function';
/**
* The console command description.
*
* @var string
*/
protected $description = "更新测试方法";
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
//编写脚本方法
}
}
Console\Kernel.php 文件中引入命令
php
//新增【必须】
use App\Console\Commands\TestConsole;
protected $commands = [
//新增【必须】
TestConsole::class,
];
//定时任务【可选,定时任务时添加】
protected function schedule(Schedule $schedule)
{
//每天1点执行一次
$schedule->command('update:test_function')->dailyAt('1:00');
}
php artisan list
测试任务是否成功 php artisan list
,列出全部任务命令,若存在【update:test_function】命令,证明任务创建成功;若不存在,证明创建失败
服务器启动定时任务
sh
#列出全部任务
crontab -l
#编辑任务
crontab -e
#增加任务
* * * * * /usr/bin/php /www/wwwroot/laucoya.com/artisan schedule:run