Laravel使用command在Linux系统中跑定时任务

一、前言

在windows系统中我们通常使用系统自带的计划任务来执行定时任务,在Linux系统中我们通常配合crontab使用shell脚本或者访问url来完成定时任务,laravel的command在Linux中使用很方便,并且laravel中的command也提供了多种时间调度方法。

二、新建command文件

执行:php artisan make:command Luckinman命令,会在app\console\commands命令下生成一个Luckinman.php的文件。

在这里插入图片描述

三、写业务逻辑

其中:

  • $signature为这个类定义一个执行名称。
  • $description为这个类增加信息描述。
  • handle方法中写自己的业务逻辑。
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Model\Student;
use Illuminate\Support\Facades\Log;

class Luckinman extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'this is test';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            Log::info('测试定时任务:'.date('Y-m-d H:i:s'));
        }catch (\Exception $e){
            Log::info($e->getMessage());
        }
    }
}

在控制台中执行该类的handle方法,使用命令:php artisan test即可。其中test$signature中定义的名称。

四、框架中配置调度频率

commands同级目录下有一个kernal.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\Luckinman::class,
    ];

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

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

$commands属性里引入我们创建的定时测试类,并在schedule方法中进行配置任务调度时间。

其中everyFiveMinutes代表每分钟执行一次。

五、Linux中开启任务调度

例如,一分钟执行一次:

* * * * * /www/server/php/73/bin/php /www/wwwroot/lars.wangchuangcode.cn/artisan schedule:run >> /dev/null 2>&1

其中,/www/server/php/73/bin/php是我php的运行配置文件所在路径,根据自己的填写。/www/wwwroot/lars.wangchuangcode.cn/是我的项目根目录。

至此,command定时调度配置成功。

如果想把crontab每次运行记录到日志中,在后面指定一个文件即可:* * * * * /www/server/php/73/bin/ php /www/wwwroot/lar.wangchuangcode.cn/artisan schedule:run >> /dev/null 2>&1 >> /www/wwwroot/1.txt

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux,我们可以使用cron和at命令来创建定时任务和延时任务。cron是一个在后台运行的守护进程,它可以根据预定的时间表执行命令或脚本。而at则是一种命令行工具,可以在指定的时间执行一次性任务。 1. 定时任务Linux系统定时任务可以使用cron命令进行配置。cron命令允许用户在指定的时间间隔内从命令行或脚本运行命令。 在cron,时间间隔由5个字段来定义: ``` * * * * * command to be executed - - - - - | | | | | | | | | +----- day of the week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of the month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) ``` 例如,要在每周一的早上5点运行一个脚本,可以使用以下命令: ``` 0 5 * * 1 /path/to/script.sh ``` 这将在每天的5:00 AM执行/path/to/script.sh脚本,只有当日期为周一时才会执行。 2. 延时任务Linux系统,我们可以使用at命令来创建延时任务。at命令允许用户在指定的时间运行一次性任务使用at命令创建一个延时任务的基本语法格式如下: ``` at TIME <<EOF command1 command2 ... EOF ``` 其TIME可以是绝对时间或相对时间,也可以使用日期和时间的组合。例如,以下命令将在10分钟后运行命令: ``` at now + 10 minutes <<EOF /path/to/command EOF ``` 此外,也可以使用at命令来指定一个具体的时间运行命令: ``` at 2:00am tomorrow <<EOF /path/to/command EOF ``` 这将在明天的凌晨2点运行命令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值