본문 바로가기

PHP-Laravel

laravel / schedule, crontab 사용방법

라라벨에서 제공하는 스케줄링 기능 

 

1. 해당 프로젝트 폴더에서 스케줄링 페이지 생성 

(생성위치 : /app/Console/Commands/클래스명.php)

php artisan make:command 클래스명

(이때, 알아서 Commands 폴더와 클래스가 생성된다)

 

 

2. class 파일에 protected $signature 부분에 스케줄링명 정의

 protected $signature = 'Scheduler:스케줄러이름';

스케줄러 이름 부분에 명명하고자하는 스케줄러명 작성

 

 

아래 전문

class 클래스이름 extends Command

{

    /**

     * The name and signature of the console command.

     *

     * @var string

     */

    protected $signature = 'Scheduler:스케줄러이름';



    /**

     * The console command description.

     *

     * @var string

     */

    protected $description = 'Command description';

 

3. public function handle() 함수 안에 동작 내용 정의

3-1. 커널에 등록 (이건 안함)

(위치 : /app/console/Karnel.php)

    protected $commands = [

       \App\Console\Commands\클래스명::class,

        \App\Console\Commands\SchedulerAACollection::class,

    ];



    protected function schedule(Schedule $schedule)
    {

         $schedule->command('스케줄러명')
         $schedule->command('Scheduler:AACollection')

    }

 

4. 스케줄러 실행

php artisan Scheduler:스케줄러명

 

5. crontab

crontab 리스트확인

crontab -l

crontab 리스트수정

crontab -e

 

반응형