简体   繁体   English

如何在共享主机上运行 Cron 作业? (流明)

[英]How to run Cron Job on Shared Hosting? (Lumen)

I am trying to implement a cron job in my lumen project.我正在尝试在我的 lumen 项目中实施一项 cron 工作。 I have BookingMaster Table when a user is creating a booking I am setting the default status to B means booked in the table.当用户创建预订时,我有 BookingMaster 表我将默认状态设置为B表示已在表中预订。 in the day of booking I am trying to update the status to I to database means In-progress.在预订当天,正在尝试将状态更新为数据库意味着进行中。 When I am doing this locally the cron is running perfectly and the status is also updating.当我在本地执行此操作时,cron 运行良好,状态也在更新。

But when I moved this code to my shared hosting it is not working any more.但是当我将此代码移动到我的共享主机时,它不再工作了。 The cron is not updating the status in database. cron 没有更新数据库中的状态。

Location Of the BookingUpdate.php is - app/Console/Commands/BookingUpdate.php BookingUpdate.php 的位置是 - app/Console/Commands/BookingUpdate.php

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;
        }


    }
}

karnel.php karnel.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 Job Command: Cron 作业命令:

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

There might be a several issues with your code not running in Cron.您的代码未在 Cron 中运行可能存在几个问题。

  • Maybe the correct path to PHP is not on the /usr/local/bin/php try running Cron with just php -q也许 PHP 的正确路径不在/usr/local/bin/php上尝试仅php -q运行 Cron
  • Some systems require the script to start with #!/usr/bin/env php or some similar combination.某些系统要求脚本以#!/usr/bin/env php或类似的组合开头。
  • There is a space in your cron command on this part artisan schedule:run so it might work once you put your command in quotation marks and escape spaces php -q "/home/rahulsco/public_html/api.pawsticks/artisan\ schedule:run" 1>> /dev/null 2>&1在这部分artisan schedule:run的 cron 命令中有一个空格,因此一旦将命令放在引号中并转义空格,它可能会起作用php -q "/home/rahulsco/public_html/api.pawsticks/artisan\ schedule:run" 1>> /dev/null 2>&1

Finally, if anything else fails I would try logging something to a file and checking after cron runs, maybe there is some other error in your directory configuration causing your script to fail before writing to database and the cron is running fine...最后,如果其他任何事情都失败了,我会尝试将某些内容记录到文件中并在 cron 运行后进行检查,可能是您的目录配置中存在一些其他错误,导致您的脚本在写入数据库之前失败并且 cron 运行良好......

In app/Console/kernel.php, the schedule function should be like this:在 app/Console/kernel.php 中,时间表 function 应该是这样的:

protected function schedule(Schedule $schedule) {
    $schedule->command('BookingUpdate:booking-update')->daily();
}

Then your BookingUpdate process will run daily at midnight.然后您的 BookingUpdate 流程将在每天午夜运行。 There are various other options to schedule your task as mentioned here: https://laravel.com/docs/7.x/scheduling#schedule-frequency-options如此处所述,还有各种其他选项可以安排您的任务: https://laravel.com/docs/7.x/scheduling#schedule-frequency-options

Also, you can simply execute the cron manually at any time using: php artisan BookingUpdate:booking-update此外,您可以随时使用以下命令手动执行 cron: php artisan BookingUpdate:booking-update

PS. PS。 Replace BookingUpdate:booking-update with $signature variable value defined in your command class you need to execute.BookingUpdate:booking-update替换为您需要执行的命令 class 中定义的$signature变量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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