【问题标题】:Laravel cronjob not executing properly on serverLaravel cronjob 无法在服务器上正确执行
【发布时间】:2017-10-16 20:33:51
【问题描述】:

我做了一个每天在 00:00 执行的 cronjob,它运行我在 Laravel check:date 中制作的 command

检查日期

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Programmefunding;
use Carbon\Carbon;
class checkDate extends Command{

protected $signature = 'check:date';

protected $description = 'Check if date is valid';


public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    Programmefunding::where('date_end', '<', Carbon::now())
    ->where('status', 'open')
    ->update(['status' => 'closed']);
}
}

该命令运行一个查询,检查状态为“开放”的计划资金,检查其日期,然后如果“date_end”值小于今天的日期,则继续将其状态更改为“关闭”。

执行 cronjob 时出现此错误

[ErrorException]


 Invalid argument supplied for foreach()

有人知道解决办法吗? 这是我在服务器上的命令:

php /home/user/public_html/artisan check:date

编辑

项目筹资模式

    <?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;


class Programmefunding extends Model
{
    use SoftDeletes;

    protected $fillable = ['status', 'name', 'description', 'permalink', 'date_start', 'date_end', 'why_end'];


    public static $enum_status = ["draft" => "Draft", "open" => "Opened", "closed" => "Closed", "announced" => "Announced", "deferred" => "Deferred"];



    /**
     * Set attribute to date format
     * @param $input
     */
    public function setDateStartAttribute($input)
    {
        if ($input != null && $input != '') {
            $this->attributes['date_start'] = Carbon::createFromFormat(config('app.date_format') . ' H:i:s', $input)->format('Y-m-d H:i:s');
        } else {
            $this->attributes['date_start'] = null;
        }
    }

    /**
     * Get attribute from date format
     * @param $input
     *
     * @return string
     */
    public function getDateStartAttribute($input)
    {
        $zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format') . ' H:i:s');

        if ($input != $zeroDate && $input != null) {
            return Carbon::createFromFormat('Y-m-d H:i:s', $input)->format(config('app.date_format') . ' H:i:s');
        } else {
            return '';
        }
    }



    /**
     * Set attribute to date format
     * @param $input
     */
    public function setDateEndAttribute($input)
    {
        if ($input != null && $input != '') {
            $this->attributes['date_end'] = Carbon::createFromFormat(config('app.date_format') . ' H:i:s', $input)->format('Y-m-d H:i:s');
        } else {
            $this->attributes['date_end'] = null;
        }
    }

    /**
     * Get attribute from date format
     * @param $input
     *
     * @return string
     */
    public function getDateEndAttribute($input)
    {
        $zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format') . ' H:i:s');

        if ($input != $zeroDate && $input != null) {
            return Carbon::createFromFormat('Y-m-d H:i:s', $input)->format(config('app.date_format') . ' H:i:s');
        } else {
            return '';
        }
    }

}

【问题讨论】:

  • 你能告诉我们Programmefunding模型吗?
  • 已添加 :) 值得注意的是,'php artisan check:date' 在本地通过 cmd 运行时有效。
  • db上的表名是否正确?不打印错误行?
  • 是的,一切都应该如此。我使用 cmd 在 localhost 上运行命令,它工作正常,但是当它通过 cronjob 运行时,我得到了错误。
  • 你是从 crontab 还是 Laravel Scheduler 调用你的脚本?

标签: php laravel cron scheduler


【解决方案1】:

经过大量挖掘,我找到了答案。

php -d register_argc_argv=On /home/user/public_html/artisan check:date

Source.

感谢大家的帮助。 :)

【讨论】:

    猜你喜欢
    • 2015-02-09
    • 1970-01-01
    • 2012-02-16
    • 2019-02-16
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    相关资源
    最近更新 更多