CRON JOB - LARAVEL - OUTPUT
Asked Answered
C

4

18

Hi I'm running CRON JOB with Laravel

Function declaration in Laravel

protected function schedule(Schedule $schedule)
{
    echo "test CRON JOB\n";
    $file1 = '1.log';
    $file2 = '2.log';
    $schedule->command('command1')->sendOutputTo($file1);
    $schedule->command('command2')->sendOutputTo($file2);

}

CRON JOB - Setting

pathToArtisan schedule:run 2>&1 >> /home/log/cron_output.log

Log file output (cron_output.log)

test CRON JOB
Running scheduled command: '/opt/alt/php55/usr/bin/php' 'artisan'command1 > '1.log' 2>&1 &
Running scheduled command: '/opt/alt/php55/usr/bin/php' 'artisan' command2 > '2.log' 2>&1 &

The echo in the function schedule is displayed but the ones inside my command 1 and command 2 are not.

I tried

echo "test"; $this->info('test');

No files 1.log or 2.log where created neither /home/log/ or where the Kernel.php file is or Command folder

Any ideas ?

Thank you

Coact answered 31/8, 2016 at 2:4 Comment(2)
Your commands should use $this->info('test'), $this->error('test'), etc. to output text.Colorist
Does not seem to change anything.Reaction
S
33

You should use the built-in task output method in Laravel.

For example:

$file = 'command1_output.log';
$schedule->command('command1')
         ->sendOutputTo($file);
Signboard answered 31/8, 2016 at 2:47 Comment(1)
Thanks. In my case I prefer appendOutputTo.Paschal
C
7

Everything is ok now.

$schedule->command('command1')
     ->sendOutputTo($file);

and inside your command

$this->info('test')

The files are created in the root folder of my application so I didn't see them !

Thank you

Coact answered 31/8, 2016 at 13:17 Comment(0)
C
1
$schedule->command('my:command')
     ->daily()
     ->onSuccess(function () {
         // The task succeeded...
//set flag here and store to DB
     })
     ->onFailure(function () {
         // The task failed...
//set flag here and store to DB
     });

Show list in admin dashboard so that you can check logs using these flags(cron status)

Crescentia answered 19/2, 2021 at 7:15 Comment(0)
C
0

you can use

$schedule->command('app:custom-command')
 ->everyDay()
->sendOutputTo(storage_path())
 ->onSuccess(function () {
  //things to do on success 
 })
 ->onFailure(function () {
     // things to do on failure
 })
->emailOutputTo("[email protected]");
Carolynncarolynne answered 20/6, 2024 at 21:19 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Magnifico

© 2022 - 2025 — McMap. All rights reserved.