一,相关的文档:
https://learnku.com/docs/laravel/10.x/scheduling/14875
二,php代码:
1,创建command:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:command OrderStatus
INFO Console command [app/Console/Commands/OrderStatus.php] created successfully.
2,编写代码:
app/Console/Commands/OrderStatus.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class OrderStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:order-status' ; //此处作为命令的名称
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description' ;
/**
* Execute the console command.
*/
public function handle()
{
//此处添加定时执行的业务逻辑
Log::channel( 'business' )->info( "定时程序运行一次" );
}
}
|
3,注册定时任务到app/Console/Kernel.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule ): void
{
//注册定时运行的程序,每分钟执行一次
$schedule ->command( 'app:order-status' )->everyMinute();
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this ->load(__DIR__. '/Commands' );
require base_path( 'routes/console.php' );
}
}
|
三,添加程序到linux的cron服务
在crontab中添加定时任务
root@lhdpc:/data/laravel/dignews# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
# * * * * * cd /data/laravel/dignews && php artisan schedule:run >> /dev/null 2>&1
* * * * * cd /data/laravel/dignews && /usr/local/soft/php8/bin/php artisan schedule:run >> /dev/null 2>&1
查看cron服务的状态:
root@lhdpc:/data/laravel/dignews# systemctl status cron.service
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-10-16 18:02:37 CST; 3 days ago
Docs: man:cron(8)
Main PID: 634 (cron)
Tasks: 1 (limit: 4582)
Memory: 13.3M
CPU: 41.340s
CGroup: /system.slice/cron.service
└─634 /usr/sbin/cron -f -P
...
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/19/laravel-ding-shi-ren-wu-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
四,测试效果
root@lhdpc:/data/laravel/dignews# tail -100 /data/laravel/logs/image-2023-10-19.log
[2023-10-19 07:04:01] local.INFO: 定时程序运行一次
[2023-10-19 07:05:01] local.INFO: 定时程序运行一次
[2023-10-19 07:06:02] local.INFO: 定时程序运行一次
[2023-10-19 07:07:01] local.INFO: 定时程序运行一次
...
五,查看laravel的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0
标签:laravel,10,Console,10.27,cron,command,定时,php
From: https://www.cnblogs.com/architectforest/p/17780045.html