PHP定时任务

PHP定时任务

PHP实现定时任务


由于公司的服务器内存太小了,担心服务器用着用着内存资源不够用了,这里从代码角度入手解决

比如用户上传图片,但是上传到服务器后并用户没有点击提交,而是退出了,那么服务器将会存在一张无用的图片占着服务器的内存资源。 这里用Laravrl自带的定时任务解决。 首先在服务器中创建定时任务

crontab -e
添加代码
* * * * * /usr/bin/php7.0 /var/www/html/laravel/artisan schedule:run >> /dev/null 2>&1

注意:/usr/bin/php7.0为你的php位置 ,* * * * *分别代表 分 时 日 月 周 (定时任务的时间) /var/www/html/laravel/为你的项目位置
查看定时任务
crontab -l

在App\Console\Commands下创建Uploadimg.php

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
use Log;
use Illuminate\Support\Facades\Storage;
use App\model\imgfilevideo;
use App\model\TaskReceipt;
class Uploadimg extends Command {

    protected $name = 'uploadimg';//命令名称

    protected $description = '删除无用图片'; // 命令描述,没什么用

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        /*删除用户 头像上传的无用文件*/
        $data=imgfilevideo::member();
        $list=array_column($data,'name');
        imgfilevideo::whereIn('name',$list)->delete();
         // 取到磁盘实例
        $disk = Storage::disk('memberimg');
        /*删除多文件*/
        $disk->delete($list);
        $str='删除文件:';
        $str.=!empty(implode(','.public_path('uploads/uploadimg/member'),$list))?public_path('uploads/uploadimg/member').implode(','.public_path('uploads/uploadimg/member'),$list):'';

         //日志
        $logFile =fopen(
            storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . 'SETTIME.log'),'a+'
        );
        chmod(storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . 'SETTIME.log'), 0666);
        fwrite($logFile, '定时任务执行时间: '.date('Y-m-d H:i:s') ."\r\n".$str. PHP_EOL);
        fclose($logFile);
    }

}

编辑 app/Console/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\Uploadimg::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('uploadimg')//
                 ->everyTenMinutes();//每10分钟执行一次
    }
}

PS:如果有多个定时任务,只需要参照test.php再次生成一个,Kernel.php中的$commands数组中再添加新加的类,schedule中$schedule->command('新name')->everyFiveMinutes();即可

->cron('* * * * *');    在自定义Cron调度上运行任务
->everyMinute();    每分钟运行一次任务
->everyFiveMinutes();   每五分钟运行一次任务
->everyTenMinutes();    每十分钟运行一次任务
->everyThirtyMinutes(); 每三十分钟运行一次任务
->hourly(); 每小时运行一次任务
->daily();  每天凌晨零点运行任务
->dailyAt('13:00'); 每天13:00运行任务
->twiceDaily(1, 13);    每天1:00 & 13:00运行任务
->weekly(); 每周运行一次任务
->monthly();    每月运行一次任务
->monthlyOn(4, '15:00');    每月4号15:00运行一次任务
->quarterly();  每个季度运行一次
->yearly(); 每年运行一次
->timezone('America/New_York'); 设置时区

执行:php artisan schedule:run 就跑起来了,将会在每10分钟去看指定文件中是否存在多余文件,多余就删除掉 了。

扫描下方二维码,关注本人公众号:filefile

___中国好码农

猜你喜欢