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