thebigk's avatar
Level 13

Purpose of $commands in Kernel.php (Task Scheduling)

I'm bit confused about the exact purpose of the $commands array in the Kernel.php - used for task scheduling.

I simply did

   protected function schedule(Schedule $schedule)
    {
         $schedule->command('inspire:me')->everyMinute();
    }

and the task was executed - without adding anything to the $commands array. Am I missing anything?

Also - I found it strange that nothing was put out on the console when the task was running. Typically, it outputs messages indicating the state of the job.

0 likes
11 replies
ohffs's avatar

It was used in previous releases (I think until 5.5 maybe) - then the framework added the ability to automagically find the commands. It's still there partly for backwards compatibility and also handy if you want to put your commands in a different directory.

As for output - it'll only print something to the console if the command has something like $this->info('Starting...') inside it.

thebigk's avatar
Level 13

Alrighty. Got it!

However, line outputs still don't work. I created a simple commande inspire:me and in the handle() method of the command, I wrote

public function handle() {

$this->info("You are the best, K!");
}

I then included the command in the scheduler : $schedule->command('inspire:me')->everyMinute();

It didn't output anything. I was expecting the console to get an entry automatically, every minute.

shez1983's avatar

you actually have to run the cron job on your system (linux/mac etc) to run artisan's 'cron job'... see the doc for more info..

thebigk's avatar
Level 13

@shez1983 - I already have the cron running. The problem is getting the output on the console open when the cron runs.

Mithrandir's avatar

In general, we don't want output of cron jobs to go to the console - most include a > /dev/null or similar in the crontab to route output to nowhere. Or a log file.

Usually, we would want our cron jobs to log what they are doing rather than output it to the console.

Are you sure your crontab command is not sending the output to nowhere?

The $this->info() in Commands is usually to provide output when the command is run through Artisan.

thebigk's avatar
Level 13

Yes, it does output properly when I run it manually; but not when artisan command is run through CRON

D9705996's avatar

If you used the default cron entry fro the documentation

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

The end part from >> is sending all output and errors to /dev/null so won't show in your terminal. Try removing everything after run. Do you now see any output? You could change /dev/null to a regular file e.g. /tmp/cron.log. do you see the output in the logs?

1 like
shez1983's avatar

instead of doing $this->info(''); why not do log::info('message') and it will end up in your log..

Snapey's avatar

The running cron job is not connected to your terminal session unless you actually launch it from that session.

Normally cron is run by the system - you cannot see what it outputs on any console. You can log it to a cron log or to the Laravel log.

Please or to participate in this conversation.