php - 如何在共享主机上运行 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
解决方案
您的代码未在 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 运行良好......
推荐阅读
- powerbi - DAX-Power BI:如何避免计算列中的循环依赖
- apache-spark - 了解 Spark 结构化流中的“附加”模式:如何删除旧数据?
- javascript - React Native Flatlist 搜索没有从 firebase 用户集合中返回任何值
- python - 使用 BeautifulSoup 抓取特定国家/地区的产品页面
- django - 删除带有数据库中模型的第三方 Django 应用程序的最佳方法是什么?
- google-oauth - 在 S3 存储桶上使用 json 文件的 Google API 的 OAuth2 问题
- wordpress - 缺少 Wordpress 多站点博客用户角色
- ios - 如何在 SwiftUI 中使用 NumberFormatter?
- asp.net-core - 如何将 .net5 应用部署到 Google Cloud Appengine?
- semantics - 在编程语言语义中如何构造一个语句来显示 S1;S2 和 S2;S3 在语义上不相等?