Issue
My laravel cron job work fine in my localhost but fail at server. Try to log for more information but failed to do so. My enviroment LOG_CHANNEL=stderr, my controller class have no issue producing log in cloudwatch.
my cron job
* * * * * cd /var/www/html && sudo -u www-data php artisan schedule:run >> /proc/1/fd/1 2>&1
my command
<?php
namespace App\Console\Commands;
use App\Models\Subscriber;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class Test2 extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:test2';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test2 Command';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
Log::debug('Command - Test');
try {
$subscribers = Subscriber::where('status', Subscriber::PENDING)->get();
return Command::SUCCESS;
} catch (Exception $e) {
Log::debug('Command failed: ' . $e->getMessage());
return Command::FAILURE;
}
}
}
log in cloudwatch
Running ['artisan' command:test2] ........... 294ms FAIL
⇂ '/usr/local/bin/php' 'artisan' command:test2 > '/dev/null' 2>&1
I can run php artisan schedule:run in server with no issue
Another thing I dont understand is why my cron is php artisan schedule:run >> /proc/1/fd/1 2>&1 but command output return '/usr/local/bin/php' 'artisan' command:test2 > '/dev/null' 2>&1
Solution
By default scheduled tasks (which run as independent processes) output to /dev/null
. To change this you can use sendOutputTo
or similar:
$schedule->command('command:test2')
->daily() // or whatever
->sendOutputTo('/proc/1/fd/1');
If you want to do this for all scheduled events you should create an event listener for the starting event and modify the scheduled event there.
Answered By - apokryfos Answer Checked By - Marilyn (WPSolving Volunteer)