* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
如果没有准备好运行命令,我们会收到以下默认输出:
# No scheduled commands are ready to run.
如何禁用此默认Laravel5消息?如果没有准备好运行的命令,我们不希望有输出。当我们能够配置该消息并返回我们自己的代码时,最好的是。
您可以在app/Console/Commands
中创建一个类似于下面的新命令,它扩展了默认的schedule:run
命令。
它会覆盖handle
方法,同时保留其他所有内容,以避免Laravel输出“没有预定的命令准备好运行”。当它没有做任何事情时。
通过使用不同的名称,无需担心冲突,如果您愿意,您仍然可以随时运行原始的php artisan schedule:run
命令。
<?php
namespace App\Console\Commands
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Console\Scheduling\ScheduleRunCommand;
class RunTasks extends ScheduleRunCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'run:tasks';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Custom task runner with no default output';
/**
* Create a new command instance.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function __construct(Schedule $schedule)
{
parent::__construct($schedule);
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
foreach ($this->schedule->dueEvents($this->laravel) as $event) {
if (! $event->filtersPass($this->laravel)) {
continue;
}
if ($event->onOneServer) {
$this->runSingleServerEvent($event);
} else {
$this->runEvent($event);
}
$this->eventsRan = true;
}
if (! $this->eventsRan) {
// Laravel would output the default text here. You can remove
// this if statement entirely if you don't want output.
//
// Alternatively, define some custom output with:
// $this->info("My custom 'nothing ran' message");
}
}
}
验证Laravel是否看到了您的新命令:
php artisan | grep run:tasks
最后更新你的cron以运行新命令:
* * * * * cd /path-to-your-project && php artisan run:tasks >> /dev/null 2>&1
正如我在评论中提到的,我看到了两种可能性
你可以通过删除你不想要的东西来filter the output
* * * * * cd /path-to-your-project && php artisan schedule:run | awk '{ if (/No scheduled commands are ready to run./ && !seen) { seen = 1 } else print }'
或者您可以使用自己的命令覆盖:
$ php artisan make:command ScheduleRunCommand
通过创建自己的命令(主要是从Schedule Run Command复制/粘贴)或扩展qazxsw poi为@ dave-s提议
如果您仍想使用新命令运行ScheduleRunCommand
,则需要在服务提供商中注册它
php artisan schedule:run
如果你看一下$this->app->extend('schedule.run', function () {
return new \App\Console\Commands\ScheduleRunCommand;
});
上Laravel的代码
https://github.com/laravel/framework/blob/78505345f2a34b865a980cefbd103d8eb839eedf/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php#L82
你看到它是通过$ this-> info handler处理的。
info处理程序在public function handle()
{
foreach ($this->schedule->dueEvents($this->laravel) as $event) {
if (! $event->filtersPass($this->laravel)) {
continue;
}
if ($event->onOneServer) {
$this->runSingleServerEvent($event);
} else {
$this->runEvent($event);
}
$this->eventsRan = true;
}
if (! $this->eventsRan) {
$this->info('No scheduled commands are ready to run.');
}
}
中定义,它调用line方法,该方法调用Command.php中定义的output handler。
所以本质上是能够拦截这个你应该能够通过绑定你自己的输出处理程序来覆盖基于run command的OutputStyle,然后再运行你在cron作业中调用的文件中的命令。
我能想到的最佳工作场景是使用symfonystyle,当字符串与目标字符串匹配时,只需返回null。
OutputFormatter
在课堂上你将按照以下方式定义:
$this->output->setFormatter( new MyCatchemAllFormatter() );
我知道我的解决方案很糟糕,我会得到大多数SO用户的支持,但是如果没有注册其他服务提供商,修改类等等,它很快就能做到。
我检查过资料,并在use Symfony\Component\Console\Formatter\OutputFormatter;
class MyCatchemAllFormatter extends OutputFormatter
{
public function formatAndWrap(string $message, int $width)
{
if($message != 'No scheduled commands are ready to run.') {
return parent::formatAndWrap($message, $width);
}
return null;
}
}
第81行发现了this
ScheduleRunCommand
“欺骗”它的最快方法是将该类复制到public function handle()
{
foreach ($this->schedule->dueEvents($this->laravel) as $event) {
if (! $event->filtersPass($this->laravel)) {
continue;
}
if ($event->onOneServer) {
$this->runSingleServerEvent($event);
} else {
$this->runEvent($event);
}
$this->eventsRan = true;
}
if (! $this->eventsRan) { // L81
$this->info('No scheduled commands are ready to run.'); // L82
}
}
,并在app/Console/ScheduleRunCommand.php
调用时每次将该文件复制到原始源路径。
1)将原始文件复制到composer dump-autoload
文件夹:
app/Console
2)在cp vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php patch/ScheduleRunCommand.php app/Console/ScheduleRunCommand.php
脚本中添加这样的行:post-autoload-dump部分:
composer.json
3)在cp app/Console/ScheduleRunCommand.php vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php
(app/Console/ScheduleRunCommand.php
)修改你的消息:
L82
4)运行:if (! $this->eventsRan) {
$this->info('NOTHING');
}
结果:
composer dump