laravel 计划任务

1.  创建计划执行文件

 执行 php artisan make:command Test
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * 逻辑代码
     *
     * @return mixed
     */
    public function handle()
    {
        //
        echo '111';
        file_put_contents("test.txt", "This is another something.", FILE_APPEND);
        //echo '111';
    }
}

2.修改 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\Test::class,
        \App\Console\Commands\Test1::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
         $schedule->command('command:name');
             //->timezone('Asia/Shanghai')
            // ->everyMinute();
         $schedule->command('command:name2');
    }

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

3.执行任务(测试可不可以成功)  command:name 是计划文件里面的 

$signature = 'command:name'

php /var/www/html/laravel/artisan command:name

4.服务器添加计划任务

sudo sudo crontab -e

#  * * * * *   /usr/bin/php5.6 /var/www/html/laravel/artisan command:name >> /dev/null 2>&1

5.也可以sh.脚本执行

* * * * * /var/www/sh/shell.sh
while true ; do
/usr/bin/php5.6  /var/www/html/laravel/artisan command:name
sleep 5
done




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值