Php 如何在共享主机上运行Cron作业?(流明)
我正试图在我的lumen项目中实现一个cron作业。我有BookingMaster表当用户创建预订时,我将默认状态设置为B表示在表中预订。在预订当天,我正在尝试将状态更新为I到正在进行的数据库。在本地执行此操作时,cron运行良好,状态也在更新 但是,当我将此代码移动到共享主机时,它就不再工作了。cron没有更新数据库中的状态 BookingUpdate.php的位置是-app/Console/Commands/BookingUpdate.phpPhp 如何在共享主机上运行Cron作业?(流明),php,laravel,cron,cpanel,lumen,Php,Laravel,Cron,Cpanel,Lumen,我正试图在我的lumen项目中实现一个cron作业。我有BookingMaster表当用户创建预订时,我将默认状态设置为B表示在表中预订。在预订当天,我正在尝试将状态更新为I到正在进行的数据库。在本地执行此操作时,cron运行良好,状态也在更新 但是,当我将此代码移动到共享主机时,它就不再工作了。cron没有更新数据库中的状态 BookingUpdate.php的位置是-app/Console/Commands/BookingUpdate.php <?php namespace App\
<?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;
}
}
}
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;
}
}
}
没有在Cron中运行的代码可能存在一些问题
- 可能PHP的正确路径不在
上,请尝试使用/usr/local/bin/PHP
PHP-q运行Cron
- 有些系统要求脚本以
或类似的组合开头/usr/bin/env php
- 此部件上的cron命令中有一个空格
,因此,一旦将命令置于引号和转义空格中,它可能会起作用artisan schedule:run
php-q”/home/rahulsco/public\u html/api.pawsticks/artisan\schedule:run“1>>/dev/null 2>&1
最后,如果其他任何操作失败,我会尝试在cron运行后将某些内容记录到文件中并进行检查,可能是目录配置中存在其他错误导致脚本在写入数据库之前失败,并且cron运行正常…可能是您的代码没有在cron中运行时出现了一些问题
- 可能PHP的正确路径不在
上,请尝试使用/usr/local/bin/PHP
PHP-q运行Cron
- 有些系统要求脚本以
或类似的组合开头/usr/bin/env php
- 此部件上的cron命令中有一个空格
,因此,一旦将命令置于引号和转义空格中,它可能会起作用artisan schedule:run
php-q”/home/rahulsco/public\u html/api.pawsticks/artisan\schedule:run“1>>/dev/null 2>&1
最后,如果其他任何操作失败,我会尝试在cron运行后将某些内容记录到文件并进行检查,可能是目录配置中存在其他错误,导致脚本在写入数据库之前失败,并且cron运行正常…在app/Console/kernel.php中,调度函数应该如下所示:
protected function schedule(Schedule $schedule) {
$schedule->command('BookingUpdate:booking-update')->daily();
}
然后您的BookingUpdate进程将在每天午夜运行。有多种其他选项可用于安排任务,如下所述:
此外,您可以随时使用以下命令手动执行cron:php artisan BookingUpdate:booking update
PS.用您需要执行的命令类中定义的变量值替换
BookingUpdate:BookingUpdate
。在app/Console/kernel.php中,调度函数应如下所示:
protected function schedule(Schedule $schedule) {
$schedule->command('BookingUpdate:booking-update')->daily();
}
然后您的BookingUpdate进程将在每天午夜运行。有多种其他选项可用于安排任务,如下所述:
此外,您可以随时使用以下命令手动执行cron:php artisan BookingUpdate:booking update
PS.用您需要执行的命令类中定义的
$signature
变量值替换BookingUpdate:BookingUpdate
。使用日志调试日志文件中没有错误。使用日志调试日志文件中没有错误。添加命令后,我在错误日志中没有发现错误,但数据库中没有错误更新。添加命令后,我在错误日志中未发现错误,但数据库未更新。