php 如何运行artisan命令schedule:在托管服务器上运行?(Laravel)

46qrfjad  于 12个月前  发布在  PHP
关注(0)|答案(3)|浏览(390)

我在xampp\htdocs\project\app\Console\Commands文件夹中有一个statusUpdate.php文件。
statusUpdate.php:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

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

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Update Job status daily';

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

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $affected = DB::table('jobs')->update(array('status' => 1));
}
}

我根据Laravel官方文档创建了它。之后,我将\App\Console\Commands\statusUpdate::class添加到位于xampp\htdocs\project\app\Console的Kernel.php文件中。
下面是Kernel.php文件中的代码:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

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

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('status:update')
             ->everyFiveMinutes();
}
}

然后我在Windows上使用CMD运行以下命令:
php artisan schedule:run
现在,它在我的本地服务器上运行得很好。我的作业表中的状态字段正确更新为1。
然而,当我将这个项目部署到共享主机并在cPanel中设置CRON作业时,它不起作用。CRON作业命令为:
php /path/to/artisan schedule:run 1>> /dev/null 2>&1
在此托管环境中,该命令不起作用。我该如何解决此问题?

h43kikqp

h43kikqp1#

好吧,我就按你说的给你答案。
Cron job命令如下:php /path/to/artisan schedule:run 1>> /dev/null 2>&1
该路径应该是在服务器中定位artisan文件的路径。就像这样:
假设artisan文件的位置是/var/www/artisan,那么简单的答案可以这样做:
php /var/www/artisan schedule:run 1>> /dev/null 2>&1
看看能不能用感谢您!

更新

这就是它应该看起来的样子。

r1wp621o

r1wp621o2#

这对我来说很好

/usr/local/bin/php /path/to/artisan schedule:run >> /dev/null 2>&1
u4dcyp6a

u4dcyp6a3#

您应该从cPanel服务器添加命令

/usr/local/bin/php /home/xyz/public_html/artisan schedule:run 1>> /home/xyz/public_html/log_laravel 2>&1

这将保留/home/xyz/public_html/log_laravel中的所有日志

Running scheduled command: '/opt/cpanel/ea-php71/root/usr/bin/php' 'artisan' SyncAPIOrders:orders > '/dev/null' 2>&1

在我的例子中,cron Job不工作,如果你打算将命令安排为每天一次(即00:00),那么$schedule->command();对象中反映相同的时间
如果命令不正确,我会在电子邮件中收到以下警告:

PHP Warning:  Module 'magickwand' already loaded in Unknown on line 0
Status: 404 Not Found
X-Powered-By: PHP/5.6.37
Content-type: text/html; charset=UTF-8

No input file specified.

Kernel.php中,应指定

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('SyncAPIOrders:orders')
               ->timezone('Asia/Kolkata')
               ->dailyAt('00:00');
}

相关问题