Environment

  • 拉威尔版本:5.1.45 (LTS)

  • PHP版本:5.6.1


Description

我正在try 使用Laravel Task Scheduling每1分钟运行一次命令.


Attempt

我已将这一行添加到我的cron选项卡文件中

* * * * * php artisan schedule:run >> /dev/null 2>&1

Here is my /app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\Inspire::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('inspire')->hourly();
        $schedule->command('echo "Happy New Year!" ')->everyMinute(); //<---- ADD HERE        }
}

I've added this line $schedule->command('echo "Happy New Year!" ')->everyMinute();


Question

我如何测试这个?

How do I trigger my echo to display ?

我怎么知道我的所作所为是否正确呢?

推荐答案

command()运行artisan命令.你想要达到的目标——向操作系统发出命令——是由exec('echo "Happy New Year!"')完成的

Testing depends on what you want to test:

  • Whether the scheduler (every minute) is working?

In this case, you don't have to. It is tested in the original framework code.

  • 命令是否成功?

Well, you can manually run php artisan schedule:run and see the output.

The scheduler does not produce any output on default (>> /dev/null 2>&1). You can, however, redirect the output of the runned scripts to any file by chaining writeOutputTo() or appendOutputTo() (https://laravel.com/docs/5.1/scheduling#task-output).


For more complex logic, write a console command instead (https://laravel.com/docs/5.1/artisan#writing-commands) and use command() - this way you can write nice, testable code.

Laravel相关问答推荐

如何在 Laravel 中使用多对一变形关系

导出所有客户端不起作用,文件保存时没有名称和扩展名

laravel 如何验证跨越午夜的两个值之间的时间(15:00 > 时间 > 01:00)

如何为 CMP 横幅安装谷歌同意脚本?

调用未定义函数 Illuminate\Encryption\openssl_decrypt()

通过单击按钮保存 PhpSpreadSheet

如何比较laravel中的两个加密(bcrypt)密码

Laravel timestamps() 不会创建 CURRENT_TIMESTAMP

Laravel Eloquent Pluck 不丢失密钥

Laravel 5.1 重命名项目

Homebrew PHP 似乎没有链接

如何在 Laravel 中使用旧输入重定向?

Heroku CLI 安装错误:PATH 未更新,原始长度 1585 > 1024

控制器外部的 Laravel 访问请求对象

Laravel Eloquent 模型中的字段

如何从 Laravel 中的资源中获取图像?

控制器中的artisan调用输出?

Laravel Nova 显示用户友好的资源名称

在 Laravel 中软删除父记录时如何软删除相关记录?

如何从 PHPUnit 测试设置运行 Laravel 数据库 seeder 机?