配置和测试Laravel任务计划 [英] Configure and Test Laravel Task Scheduling
问题描述
-
Laravel版本: 5.1.45(LTS)
PHP版本: 5.6.1
我正在尝试使用Laravel每1分钟运行一条命令任务计划.
I'm trying to run a command every 1 minute using Laravel Task Scheduling.
我已将此行添加到我的cron选项卡文件中
I've added this line to my cron tab file
* * * * * php artisan schedule:run >> /dev/null 2>&1
这是我的/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 }
}
我已添加此行$schedule->command('echo "Happy New Year!" ')->everyMinute();
我该如何测试?
如何触发我的回声显示?
How do I trigger my echo to display ?
我怎么知道我做错了什么?
How do I know if what I did is not wrong ?
此刻我正在征求任何建议.
I'm opening to any suggestions at this moment.
任何对此的提示/建议/帮助将不胜感激!
Any hints / suggestions / helps on this be will be much appreciated !
推荐答案
command()
运行工匠命令.您要实现的目标-向OS发出命令-由exec('echo "Happy New Year!"')
command()
runs an artisan command. What you're trying to achieve - issuing a command to the OS - is done by exec('echo "Happy New Year!"')
测试取决于您要测试的内容:
Testing depends on what you want to test:
- 调度程序(每分钟)是否正常工作?
在这种情况下,您不必这样做.已在原始框架代码中进行了测试.
In this case, you don't have to. It is tested in the original framework code.
- 命令是否成功?
好吧,您可以手动运行php artisan schedule:run
并查看输出.
Well, you can manually run php artisan schedule:run
and see the output.
默认情况下(>> /dev/null 2>&1
),调度程序不产生任何输出.但是,您可以通过链接writeOutputTo()
或appendOutputTo()
( https://laravel.com/docs/5.1/scheduling#task-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).
有关更复杂的逻辑,请编写控制台命令( https://laravel.com /docs/5.1/artisan#writing-commands )并使用command()
-这样,您就可以编写漂亮的,可测试的代码.
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任务计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!