Laravel 定时任务调度 的 Artisan 命令调度

1、创建命令

php artisan make:command command_name --command=artisan_command_name
# Explanation:
# command_name: 生成的文件名
# artisan_command_name: php artisan 命令调度时的命令名称
# 结果: 在 /app/Console/Commands/ 下生成名为 command_name.php 的文件
# Example:
# php artisan make:command BussineSendSms --command=BussineSendSms
# 生成的文件名:BussineSendSms
# 调度时的命令名称:BussineSendSms

执行后会提示Console command created successfully.

执行PHP artisan list会看到对应的执行command命令说明已成功.

2、测试刚才生成的命令是否OK

php artisan BusineSendSms
# Explanation:
# 没有返回则表示成功。
# 因为 /app/Console/Commands/BusineSendSms.php 的 handle 方法中没有写内容。写了就会有返回。

3、编辑生成的文件 /app/Console/Commands/BusineSendSms.php 的 handle 方法

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class BusineSendSms extends Command
{
    /**
     * The name and signature of the console command.
     * 用来描述命令的名字与参数
     * @var string
     */
    protected $signature = 'BusineSendSms';//这里一定要和命令中对应要不然会报错
    /**
     * The console command description.
     * 存储命令描述
     * @var string
     */
    protected $description = 'Command description';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     * 执行命令进行的操作
     * @return mixed
     */
    public function handle()
    {
        // 这里是任务的具体处理
    }
}
4、编辑 App\Console\Kernel.php 文件,添加调度
# 先到 /app/Console/Kernel.php 中 $commands 数组中进行注册。
# 然后在 /app/Console/Kernel.php 的 schedule 方法中定义调度任务。
<?php
namespace App\Console;
use App\Console\Commands\BusineSendSms;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     * 你的应用程序提供的Artisan命令。
     * @var array
     */
    protected $commands = [
        BusineSendSms::class
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        // 每分钟执行一次获取领导信箱
        // command() 调度时的命令名称
        // everyFiveMinutes() 调度规则
        // appendOutputTo() 调度命令进行操作的返回结果记录文件
        $schedule->command('BusineSendSms')->everyFiveMinutes()->appendOutputTo(base_path('storage/crontab/log.log'));
    }
    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');
        require base_path('routes/console.php');
    }
}

5、编辑机器的定时任务 crontab

  1. # path-to-your-php/bin/php 是你的PHP的绝对路径,通过 which php 可以得到;
  2. # path-to-your-project/artisan 是你项目中Laravel框架中根目录下的 artisan 的绝对路径;
    * * * * * path-to-your-php/bin/php /path-to-your-project/artisan BusineSendSms >> /dev/null 2>&1

    如果命令执行比较频繁(如每分钟一次),或者命令输出内容较多,会使这个邮件文件不断追加内容,文件越来越大。而邮件文件一般存放在根分区,根分区一般相对较小,所以会造成根分区写满而无法登录服务器。

    不输出内容

    * * * * * path-to-your-php/bin/php /path-to-your-project/artisan BusineSendsms >> /dev/null 2>&1

    将正确和错误日志都输出到 /tmp/log.log

    * * * * * path-to-your-php/bin/php /path-to-your-project/artisan BusineSendSms > /tmp/log.log 2>&1

    只输出正确日志到 /tmp/log.log

    * * * * * path-to-your-php/bin/php /path-to-your-project/artisan BusineSendSms > /tmp/log.log &

    只输出错误日志到 /tmp/log.log

    * * * * * path-to-your-php/bin/php /path-to-your-project/artisan BusineSendSms 2> /tmp/log.log &

     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值