問題描述
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.
這篇關于配置和測試 Laravel 任務調度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!