laravel定時任務

在項目中遇到定時執行某個php腳本,好比發短信 發郵件之類的php

那麼該如何寫呢?html

學習源頭:python

發送郵件:http://laravelacademy.org/post/1986.htmlmysql

send方法返回的是空,要用count(Mail::failures()) > 0來判斷纔對。詳見http://stackoverflow.com/questions/35569861/how-to-check-email-sent-or-not-in-laravel-5,文檔 https://laravel.com/api/5.1/Illuminate/Mail/Mailer.html#method_failureslinux

注意這裏的$flag其實返回的是null;要根據count(Mail::failures()) > 0 時爲失敗, 輸出日誌信息laravel

導出excel:http://laravelacademy.org/post/2024.htmlweb

這裏只須要生成excel便可,不須要導出:sql

Excel::create('學生成績',function($excel) use ($cellData){
     $excel->sheet('score', function($sheet) use ($cellData){
         $sheet->rows($cellData);
     });
//})->store('xls')->export('xls');
})->store('xls'); // 把導出去掉就行

定時任務:http://laravelacademy.org/post/6228.htmlshell

https://blog.csdn.net/zhezhebie/article/details/79205414json

這裏用不用php artisan均可,也能夠本身依據示例來建立

<?php
/**
 * 定時發送郵件(預警管理數據報表)
 * User: djw
 * Date: 2018/6/25
 * Time: 17:14
 */

namespace iqiyi\Console\Commands;


use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Mail;
use Maatwebsite\Excel\Facades\Excel;

class SendWarningLists extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send.warning.lists'; // 這裏很重要 這是未來執行crontab 是對應的命令名稱   不能亂寫
  // */1 * * * * /opt/php7/bin/php /home/www/stock/crm/web1/artisan send.warning.lists >> /www/logs/stock.send.warning.lists.log
  // 有參數的寫法爲
  //
protected $signature = 'stock.card {do : do what} {do_param : param} ';
  // protected $signature = 'expdown {datas}';
  
/** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() // 處理的代碼寫這裏 { $list = \iqiyi\Http\Controllers\Warning::listImport(); // 預警管理-庫存 //dd($list); $list2 = \iqiyi\Http\Controllers\Warning::listExport(); // 渠道庫存 // 生成excel $menu_arr = [ 'import_types'=>'卡密類型', 'card_count'=>'總數量', 'remain_count'=>'剩餘數量', 'phone'=>'電話', 'user'=>'負責人', 'num'=>'預警數量', ]; // 第一個excel的表頭數組 $menu_arr2 = [ 'channel'=>'卡密渠道', 'import_types'=>'卡密類型', 'card_count'=>'總數量', 'remain_count'=>'剩餘數量', 'phone'=>'電話', 'user'=>'負責人', 'num'=>'預警數量', ]; // 第二個excel的表頭數組 // 第一行的表頭 foreach($menu_arr as $k=>$v){ $cellData[0][] = $v; } foreach($menu_arr2 as $k=>$v){ $cellData2[0][] = $v; } $list = json_decode(json_encode($list), true); $list2 = json_decode(json_encode($list2), true); // 數據信息 if (!empty($list)) { $i = 1; foreach($list as $k=>$v){ $cellData[$i][] = isset($v['import_types']) ? $v['import_types'] :''; $cellData[$i][] = isset($v['card_count']) ? $v['card_count'] :''; $cellData[$i][] = isset($v['remain_count']) ? $v['remain_count'] :''; $cellData[$i][] = isset($v['war']['phone']) ? $v['war']['phone'] : ''; $cellData[$i][] = isset($v['war']['user']) ? $v['war']['user'] : ''; $cellData[$i][] = isset($v['war']['num']) ? $v['war']['num'] : ''; $i++; } } if (!empty($list2)) { $y = 1; foreach($list2 as $k=>$v){ $cellData2[$y][] = isset($v['channel']) ? $v['channel'] :''; $cellData2[$y][] = isset($v['import_types']) ? $v['import_types'] :''; $cellData2[$y][] = isset($v['card_count']) ? $v['card_count'] :''; $cellData2[$y][] = null !== ($v['card_count'] - $v['back_num'] - $v['surplus']) ? ($v['card_count'] - $v['back_num'] - $v['surplus']) :''; $cellData2[$y][] = isset($v['war']['phone']) ? $v['war']['phone'] : ''; $cellData2[$y][] = isset($v['war']['user']) ? $v['war']['user'] : ''; $cellData2[$y][] = isset($v['war']['num']) ? $v['war']['num'] : ''; $y++; } } // 保存爲xls文件 Excel::create(date('Y-m-d')."預警管理-庫存 數據報表",function($excel) use ($cellData){ $excel->sheet('數據報表', function($sheet) use ($cellData){ $sheet->rows($cellData); }); })->store('xls'); // 文件默認保存到storage/exports目錄下 Excel::create(date('Y-m-d')."渠道庫存 數據報表",function($excel) use ($cellData2){ $excel->sheet('數據報表', function($sheet) use ($cellData2){ $sheet->rows($cellData2); }); })->store('xls'); // 文件默認保存到storage/exports目錄下 $str = date('Y-m-d').'預警管理 數據報表'; $toarr = Config::get('constants.WLRECEIVER'); foreach ($toarr as $key => $value) { // 這裏$flag返回爲null 即便成功 $flag = Mail::send('mail.sendwarninglists', ['str' => $str], function ($message) use($value){ $to = $value; $message->to($to)->subject(date('Y-m-d').'預警管理 數據報表'); $attachment = storage_path('exports/'.date('Y-m-d')."預警管理-庫存 數據報表.xls"); $attachment2 = storage_path('exports/'.date('Y-m-d')."渠道庫存 數據報表.xls"); // chmod($attachment , 0644); //0644 要修改爲的權限值 // chmod($attachment2 , 0644); //0644 要修改爲的權限值 //在郵件中上傳附件 $message->attach($attachment,['as'=>date('Y-m-d').'預警管理-庫存 數據報表.xls']); $message->attach($attachment2,['as'=>date('Y-m-d').'渠道庫存 數據報表.xls']); }); if(count(Mail::failures()) > 0){ // 獲取失敗收件人的數組 echo "\r\n".date('Y-m-d')."{$value}發送郵件失敗,請重試!"; // 輸出的內容 }else{ echo "\r\n".date('Y-m-d')."{$value}發送郵件成功,請查收!"; } } } }

而後須要在Kernel.php中添加新寫的執行代碼

<?php

namespace iqiyi\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 = [       
        \iqiyi\Console\Commands\importCard::class,
        \iqiyi\Console\Commands\StockCard::class,
        \iqiyi\Console\Commands\expon::class,
        \iqiyi\Console\Commands\expdown::class,
        \iqiyi\Console\Commands\Warning::class,
        \iqiyi\Console\Commands\OrderBd::class,
        \iqiyi\Console\Commands\lostorder::class,
        \iqiyi\Console\Commands\monitorqueue::class,
        \iqiyi\Console\Commands\Card::class,
        \iqiyi\Console\Commands\ChannelPriceTip::class,
        \iqiyi\Console\Commands\PCard::class,
        \iqiyi\Console\Commands\SendWarningLists::class,    ];

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

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

而後就是再linux上crontab -e

編輯你的定時任務了

0 1 * * * /usr/bin/python /opt/scripts/bakup/bakupwiki.py root 123uway123 wiki >/tmp/mysqllog.txt 2>&1
00 0 * * * /opt/php7/bin/php /www/fee/sandbox/web/artisan monitor
0 3 * * * /www/shell/iqiyi/del.bak.sql.sh >> /www/logs/del.log
#餘額提醒
#*/1 * * * * /opt/php7/bin/php /home/www/stock/crm/web1/artisan channel.price.tip >> /www/logs/stock.channel.price.tip.log
#庫存補單
#*/1 * * * * /opt/php7/bin/php /home/www/stock/crm/web1/artisan order.bd >> /www/logs/stock.order.bd.log
00 0 * * * /opt/scripts/logrotate/logrotate.sh  > /dev/null 2>&1

#iqiyi 日誌 每小時切割
0 */1 * * * /opt/scripts/logrotate/logrotate.hourly.sh  > /dev/null 2>&1
#iqiyi 日誌處理
30 * * * * /opt/python3.6/bin/python3 /home/offline/uvpv/index.py  > /dev/null 2>&1
#iback圖片備份
0 2 * * * sh /home/iqiyi/bak/iback.bak.sh  > /dev/null 2>&1
#預警管理數據報表每個月26日0:00發送郵件
#*/1 * * * * /opt/php7/bin/php /home/www/stock/crm/web1/artisan send.warning.lists >> /www/logs/stock.send.warning.lists.log

大功告成

相關文章
相關標籤/搜索