5

I have a command named MyCommand, and I am calling it from a job named MyJob. I cannot see the command outputs when it's called from job. But if I run the command directly from command line, command output is seen.

MyCommand.php code:

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyCommand extends Command
{

    protected $signature = 'mycommand:doit';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $this->info('Process started');

        //Some process is done here

        $this->info('Process completed');
    }
} 

MyJob.php code:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Artisan;

class MyJob implements ShouldQueue
{
    public function __construct()
    {

    }

    public function handle()
    {
        Artisan::call('mycommand:doit');
    }
} 
Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
Umut KIRGÖZ
  • 2,105
  • 3
  • 22
  • 29

2 Answers2

4

As in theory, you are not in a terminal with you run your job (Job may for example, be queued or scheduled), output is not saved when run outside a terminal.

However, you can still get the output buffer with Artisan::output();

Example:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Artisan;

class MyJob implements ShouldQueue
{
    public function __construct()
    {

    }

    public function handle()
    {
        Artisan::call('mycommand:doit');
        $output = Artisan::output(); // $output is a string

        // Do whatever you want with $output
    }
}

Update: synchronous output

You may try this: Example of command:

class SlowCommand extends Command
{
    protected $signature = "slow";


    public function handle()
    {
        $max = 10;

        for ($i = 0; $i < $max; $i++) {
            $this->line($i);
            sleep(1);
        }
    }
}
// Synchronous output
Artisan::call("slow"); 
echo Artisan::output();

// Asynchronous output
$buffer = new ConsoleOutput();
Artisan::call("slow", [], $buffer);
Mathieu Bour
  • 666
  • 4
  • 23
  • This works, but it returns the output when command is completed. I want to get the real time infos, notices and errors when they occured, not whole command is completed. I partially find a way to do this, instead of using $this->info, I directly echo the information text. – Umut KIRGÖZ May 29 '19 at 12:26
  • Yes of course, this is a synchronous piece of code. But since you did not precised the paradigm of your Artisan calls, I did not tried to be real-time. – Mathieu Bour May 29 '19 at 12:30
  • 1
    @UmutKIRGÖZ I tried to include asynchronous output, as you requested. – Mathieu Bour May 29 '19 at 12:54
0

use Artisan::output method to get output from last executed command by Artisan facade.

Vitalii
  • 1,137
  • 14
  • 25