laravel 任务调度创建,执行成功

一代码

1.App\Console\Kernel.php中编写laravel代码


protected function schedule(Schedule $schedule)

{

    $schedule->command('user:vip')->withoutOverlapping()->daily();;

}


schedule方法中调用指定的任务


2 添加注册脚本


protected $commands = ['App\\Console\\Commands\\UserVip'];


3创建任务UserVip,在App\Console\Commands/ 下新建UserVip.php文件,如下,有模板就用模板,没有就用命令创建。(进入项目路径addons/shimmer_liveshop,有artisan文件的目录下执行,php artisan make:command UserVip会在App\Console\Commands\下生成UserVip.php文件)


namespace App\Console\Commands;

use Illuminate\Console\Command;

class SevenTime extends Command

{

    /**

* The name and signature of the console command.

*

    * @var string

*/

    protected $signature = 'command:name';

    /**

* The console command description.

*

    * @var string

*/

    protected $description = 'Command description';

    /**

* Create a new command instance.

*

    * @return void

*/

    public function __construct()

{

        parent::__construct();

    }

private function userVipOver(){

//业务

}

    /**

* Execute the console command.

*

    * @return mixed

*/

    public function handle()

{

        $this->userVipOver();

    }

}


如果是手动复制模板,记得修改


在handle()方法中调用自定义方法,

二。服务器执行

在命令行输入

crontab -e

添加如下代码

* * * * * /usr/local/php/bin/php /mnt/wwwroot/addons/shimmer_liveshop/artisan schedule:run >> /dev/null 2>&1


保存后   执行   php artisan schedule:run       在artisan  的目录下执行哦

注意1,我遇到执行上面命令后只执行一次,是php环境没配置好,所以改为上面的  /usr/local/php/bin/php   教程上是直接  php  /mnt/wwwroot/addons/shimmer_liveshop/artisan schedule:run >> /dev/null 2>&1

注意2, 如果执行命令,任务没有执行,返回代码部分检查问题

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容