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

How to run artisan command schedule:run on hosting server? (Laravel)

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

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, class in Kernel.php on xampp\htdocs\project\app\Console folder.

这里是 karnel.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();
}
}

然后我是运行

php artisan schedule:run command using CMD on windows 7.

现在它工作正常(在本地服务器中)。我的工作 table 状态字段由 1.

正确更新

但是当我在共享主机上部署这个项目并在 cPanel 中为我的服务器添加一个 CRON 命令时:

Cron 作业命令是这样的:php /path/to/artisan schedule:run 1>> /dev/null 2>&1

现在在这种情况下命令不起作用,这就是问题所在。我该如何解决?

嗯。我按照你说的给你答案。

Cron job command is like this : 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

检查一下是否有效。谢谢!

更新:

它应该是这样的。

这对我来说效果很好

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

您应该将来自 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)当且仅当,同样的时间是 not 反映在 $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');
}