4

I have custom Artisan commands that run locally as well as on my production server when I am SSH'd in, but are unavailable to any cron jobs. I've even tried running it as the user the cron job runs as and it works fine from my console.

When I run php artisan in the above settings, my custom commands are listed and available. However, they are not listed when I run php artisan as a cron job.

Furthermore, trying to run the custom command php artisan subjects:calculate as a cron job results in the following error:

[InvalidArgumentException]
There are no commands defined in the "subjects" namespace.

Laurence
  • 58,936
  • 21
  • 171
  • 212
Dwight
  • 12,120
  • 6
  • 51
  • 64

4 Answers4

13

I was fighting with the same error and I found the solution. First failed attempts

*/5 * * * * /usr/bin/php /home/mysite/public_html/artisan my:command

*/5 * * * * php /home/mysite/public_html/artisan my:command

Solution

*/5 * * * * /usr/local/bin/php /home/mysite/public_html/artisan my:command
rterrani
  • 526
  • 2
  • 10
  • That's the one! I thought I had posted my answer here when I found it but I must have forgotten. Actually specifying the full path to php on the server and then the full path to the file made it work. – Dwight Feb 13 '14 at 01:29
  • I had godaddy hosting and this answer resolved my issue as well. I was working on laravel 4 for conjob and got resolved by his command. May I know what is `/local/` keyword is required in the path? – Rajan Rawal Feb 07 '15 at 06:11
  • Just find where your PHP binary is installed by using `which php` and then reference it in that location. – Dwight Feb 17 '15 at 00:17
  • What is the logical answer for this issue? Looks like the php command aims the same place. Why this works so wild!? This solves also mine problem - but I'm curious why is like that? – Filip Koblański May 28 '18 at 12:19
1

Be sure to add the command to app/start/artisan.php file:

Artisan::add(new SubjectsCommand);

or if you are using the IOC container:

Artisan:resolve('SubjectsCommand');

Then run the CronJob from the folder of the app:

00 09-18 * * 1-5 php /path/to/yourapp/artisan subjects:calculate

or

00 09-18 * * 1-5 /usr/bin/php /path/to/yourapp/artisan subjects:calculate
Joe
  • 351
  • 2
  • 8
  • The command is set up and works properly from the command line. It only doesn't work when being accessed through a cron job. – Dwight Jan 08 '14 at 12:28
1

At least for me that worked:

class Kernel extends ConsoleKernel
{
    protected $commands = [
        // Commands\YourCommand::class,
    ];
}

Just added my command to the list of command kernel.

glaucomorais
  • 317
  • 3
  • 9
0

You probable need to make sure artisan is running from the correct directory

cd /path/to/yourproject && php artisan subjects:calculate
rtconner
  • 1,221
  • 12
  • 19
  • It is running artisan from the correct directory, running `php artisan` does list all the commands, but my custom ones aren't available. The actual cron runs `php /path/to/myproject/artisan subjects:calculate`. – Dwight Jan 08 '14 at 07:00