首页 > 解决方案 > 如何在共享主机上运行 Cron 作业?(流明)

问题描述

我正在尝试在我的 lumen 项目中实施一项 cron 工作。当用户创建预订时,我有 BookingMaster 表我将默认状态设置为B表示已在表中预订。在预订当天,我正在尝试将状态更新为数据库意味着进行中当我在本地执行此操作时,cron 运行良好,状态也在更新。

但是当我将此代码移动到我的共享主机时,它不再工作了。cron 没有更新数据库中的状态。

BookingUpdate.php 的位置是 - app/Console/Commands/BookingUpdate.php

BookingUpdate.php

<?php

namespace App\Console\Commands;

use Helpers;
use Illuminate\Console\Command;
use App\BookingMaster;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Cron for Booking Update';

    public static $process_busy = false;

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(){
        if (self::$process_busy == false) {
            self::$process_busy = true;
            $where['status'] = 'B';
            $update = BookingMaster::updateRecord(6,$where);
            self::$process_busy = false;             
            echo 'Done';
               return true;
        } else {
            if ($debug_mode) {
                error_log("Process busy!", 0);
            }

            return false;
        }


    }
}

卡内尔.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

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

    ];

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

Cron 作业命令:

/usr/local/bin/php -q /home/rahulsco/public_html/api.pawsticks/artisan schedule:run 1>> /dev/null 2>&1

标签: phplaravelcroncpanellumen

解决方案


您的代码未在 Cron 中运行可能存在几个问题。

  • 也许 PHP 的正确路径不是/usr/local/bin/php尝试运行 Cronphp -q
  • 一些系统需要脚本开头#!/usr/bin/env php或一些类似的组合。
  • 这部分的 cron 命令中有一个空格,artisan schedule:run因此一旦将命令放在引号中并转义空格,它可能会起作用 php -q "/home/rahulsco/public_html/api.pawsticks/artisan\ schedule:run" 1>> /dev/null 2>&1

最后,如果其他任何事情都失败了,我会尝试将某些内容记录到文件中并在 cron 运行后进行检查,可能是您的目录配置中存在一些其他错误,导致您的脚本在写入数据库之前失败并且 cron 运行良好......


推荐阅读