Laravel 5 日程安排不工作
Laravel 5 schedule not working
我的 laravel 版本是 5.0.28,我在 cloud9 上构建,我在我的 cron 中添加了这个命令:
#!/bin/bash
PATH=/usr/bin
* * * * * php /home/ubuntu/workspace/app/artisan scheduled:run 1>> /dev/null 2>&1
我在 Kernel.php
上添加了这段代码。我引用了这个网站:https://laravel-news.com/2014/11/laravel-5-scheduler/
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controllers\ApiController;
class Kernel extends ConsoleKernel {
protected $commands = [
'App\Console\Commands\Inspire',
];
protected function schedule(Schedule $schedule)
{
$schedule->call('ApiController@test_job')->hourly();
}
}
我等了还是不行,所以我尝试使用命令php artisan schedule:run
,我得到:No scheduled commands are ready to run.
我搜索并找到了这个答案:
所以我修改了我的代码。另外,这段代码没有指定时间,所以我修改了我的 cron
来指定时间,但它仍然不起作用。我没有更多的想法。请帮忙。谢谢。
代码
$schedule->call(join('@', [ApiController::class, 'test_job']));
cron
0 0,3,6,9,12,15,18,21 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
30 1,4,7,10,13,16,19,22 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
Laravel scheduler 适用于命令,不适用于控制器方法:
- 创建命令:
php artisan make:command PurchasePodcast
- 编辑命令:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PurchasePodcast extends Command
{
protected $name = 'purchase:podcast';
public function fire()
{
// do stuff here
}
}
- 将命令添加到
Console\Kernel.php
:
protected $commands = [
'App\Console\Commands\PurchasePodcast',
];
- 在调度程序中使用命令:
$schedule->command('purchase:podcast')->hourly();
为了完成@limonte 的回答,创建控制台命令如下:
php artisan make:console CampaignsCollect --command=campaigns:collect
此处参考:link
首先测试 cron 是否在您的服务器或本地主机中 运行ning 类型:
> sudo service cron status
如果没有安装:
> sudo apt-get install cron
启用 laravel 的调度程序:
> crontab -e
如果没有vim,您可以select编辑器直接打开。请务必在底部输入此行:
* * * * * php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
测试您是否已在 laravel 调度程序中正确设置,运行 来自您的项目文件夹:
>php artisan schedule:run
这应该会执行任务并告诉您正在做什么。
查找原因有几个方面:
首先检查config/app.php中的'timezone'是否设置正确。 Laravel 将重置时区,即使您已经在 php.ini 中配置了它。
其次,检查 crontab 是否按预期工作。当您收到消息 "No schedule to be ready" 时,这意味着您的 crontab 是 运行 并且可以检测到 php 和 artisan 命令。
在我的例子中,调度程序实际上 运行 但由于 较低的 php 版本而遇到错误,除了 artisan 路径(这是您的项目文件夹),我必须设置 php 路径如下:
* * * * * /path_to_php_folder/bin/php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
我的 laravel 版本是 5.0.28,我在 cloud9 上构建,我在我的 cron 中添加了这个命令:
#!/bin/bash
PATH=/usr/bin
* * * * * php /home/ubuntu/workspace/app/artisan scheduled:run 1>> /dev/null 2>&1
我在 Kernel.php
上添加了这段代码。我引用了这个网站:https://laravel-news.com/2014/11/laravel-5-scheduler/
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controllers\ApiController;
class Kernel extends ConsoleKernel {
protected $commands = [
'App\Console\Commands\Inspire',
];
protected function schedule(Schedule $schedule)
{
$schedule->call('ApiController@test_job')->hourly();
}
}
我等了还是不行,所以我尝试使用命令php artisan schedule:run
,我得到:No scheduled commands are ready to run.
我搜索并找到了这个答案:
所以我修改了我的代码。另外,这段代码没有指定时间,所以我修改了我的 cron
来指定时间,但它仍然不起作用。我没有更多的想法。请帮忙。谢谢。
代码
$schedule->call(join('@', [ApiController::class, 'test_job']));
cron
0 0,3,6,9,12,15,18,21 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
30 1,4,7,10,13,16,19,22 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
Laravel scheduler 适用于命令,不适用于控制器方法:
- 创建命令:
php artisan make:command PurchasePodcast
- 编辑命令:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PurchasePodcast extends Command
{
protected $name = 'purchase:podcast';
public function fire()
{
// do stuff here
}
}
- 将命令添加到
Console\Kernel.php
:
protected $commands = [
'App\Console\Commands\PurchasePodcast',
];
- 在调度程序中使用命令:
$schedule->command('purchase:podcast')->hourly();
为了完成@limonte 的回答,创建控制台命令如下:
php artisan make:console CampaignsCollect --command=campaigns:collect
此处参考:link
首先测试 cron 是否在您的服务器或本地主机中 运行ning 类型:
> sudo service cron status
如果没有安装:
> sudo apt-get install cron
启用 laravel 的调度程序:
> crontab -e
如果没有vim,您可以select编辑器直接打开。请务必在底部输入此行:
* * * * * php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
测试您是否已在 laravel 调度程序中正确设置,运行 来自您的项目文件夹:
>php artisan schedule:run
这应该会执行任务并告诉您正在做什么。
查找原因有几个方面: 首先检查config/app.php中的'timezone'是否设置正确。 Laravel 将重置时区,即使您已经在 php.ini 中配置了它。 其次,检查 crontab 是否按预期工作。当您收到消息 "No schedule to be ready" 时,这意味着您的 crontab 是 运行 并且可以检测到 php 和 artisan 命令。
在我的例子中,调度程序实际上 运行 但由于 较低的 php 版本而遇到错误,除了 artisan 路径(这是您的项目文件夹),我必须设置 php 路径如下:
* * * * * /path_to_php_folder/bin/php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1