简体   繁体   中英

Custom Laravel Artisan command not available through cron job

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.

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

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

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.

您可能需要确保工匠从正确的目录运行

cd /path/to/yourproject && php artisan subjects:calculate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM