配置和测试 Laravel 任务调度
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 AppConsole;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
AppConsoleCommandsInspire::class,
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $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 ?
推荐答案
command()
运行工匠命令.您要实现的目标 - 向操作系统发出命令 - 由 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.
相关文章