laravel cron工作不起作用 [英] laravel cron job not working

查看:76
本文介绍了laravel cron工作不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的laravel cron作业无法自动运行.我对其进行了测试,并且似乎可以正常工作,当我使用php artisan checkCommissions:check_commissions时,它可以完美地运行我的任务.所以我认为问题出在我用cpanel编写的命令中,所以有什么帮助吗?

my laravel cron job it does not work automatically. I test it and it seems to work, when I php artisan checkCommissions:check_commissions it runs my task perfectly. so I think the problem is with the command I wrote in cpanel so any help ?

app \ Console \ Commands \ CheckCommissions.php:

app\Console\Commands\CheckCommissions.php:

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\User;
use App\Models\Setting;
use App\Models\Commission;

class checkCommissions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'checkCommissions:check_commissions';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check users counters and give them commissions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $maxout = Setting::where('name', 'maxout')->first();
        if (!is_null($maxout)) {
            $maxout = $maxout->value;
        }

        if (is_null($maxout) || !is_numeric($maxout) || $maxout < 0) {
            $maxout = 0;
        }

        $commission = Setting::where('name', 'commission')->first();
        if (!is_null($commission)) {
            $commission = $commission->value;
        }

        if (is_null($commission) || !is_numeric($commission) || $commission < 0) {
            $commission = 0;
        }

        $users = User::all();

        foreach ($users as $user) {
            if ($user->counter_right >= $maxout && $user->counter_left >= $maxout) {
                $num = $maxout;
                $update_counters = User::where('id', $user->id)->update(['counter_left' => 0, 'counter_right' => 0]);
            } else {
                if ($user->counter_right >= $user->counter_left) {
                    $num = $user->counter_left;
                } else {
                    $num = $user->counter_right;
                }

                $update_counters = User::where('id', $user->id)->update(['counter_left' => $user->counter_left - $num, 'counter_right' => $user->counter_right - $num]);
            }

            if($num > 0) {
                $money = $num * ($commission / 2);

                $update_user = User::where('id', $user->id)->update(['money' => $user->money + $money]);

                Commission::insert(['user_id' => $user->id, 'money' => $money]);
            }
        }
    }
}

app \ Console \ kernel.php:

app\Console\kernel.php:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        '\App\Console\Commands\checkCommissions',
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();

        $schedule->command('checkCommissions:check_commissions')
                 ->everyMinute(); //
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

以及Cpanel中的命令:

and the command in Cpanel:

分钟小时月份月份工作日命令操作

Minute Hour Day Month Weekday Command Actions

0 0 * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan checkCommissions:check_commissions >> /dev/null 2>&1

推荐答案

首先,欢迎使用StackOverflow.是的,您的命令是错误的...您想在应用程序控制台内核中运行计划的命令,而不是计划命令执行.请阅读有关任务计划的Laravel官方文档(也许还有

First of all Welcome to StackOverflow. Yes your command is wrong... You want to run a scheduled command within the application console kernel, not scheduling a command execution. Please read the Laravel official documentation about Task scheduling (and maybe also the cron wiki).

您只是告诉CPanel crontab每天在午夜运行命令 checkCommissions:check_commissions ,但是您希望在调度程序中每分钟运行该命令.

You're simply telling to the CPanel crontab to run the command checkCommissions:check_commissions everyday at midnight but in your scheduler you want to run that command every minute.

首先,您必须从以下位置更新您的cronjob

First of all you have to update your cronjob from

0 0 * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan checkCommissions:check_commissions >> /dev/null 2>&1

* * * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan schedule:run >> /dev/null 2>&1

为了启动调度程序.然后laravel将处理其余的事情.

In order to start the scheduler. Then laravel will handle the rest.

这篇关于laravel cron工作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆