每当我运行artisan命令时,每次都会运行Cron作业

时间:2016-11-10 13:25:50

标签: php laravel cron scheduled-tasks

我有一个任务计划运行一个工匠命令,每当我运行artisan命令时,它每次执行都会执行,而不管我给cron工作的时间。

这是我正在使用的控制台

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

/**
 * 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()
{       
        //executing function;
}
      $this->info('Remainder SMS send successfully !!');
}  
} 

这是我的console / kernel.php文件

class Kernel extends ConsoleKernel
{

protected $commands = [
    \App\Console\Commands\Inspire::class,
    \App\Console\Commands\FileEntryData::class,
    \App\Console\Commands\CronJobConsole::class,
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('sms:note')->dailyAt('19:00')->sendOutputTo('cronjob-result.php');
}
}

当我运行php artisan短信时:注意它每次都在执行。我想让它在我有gven的特定时间执行。

请帮帮我。

1 个答案:

答案 0 :(得分:3)

这个问题,正如你指定的那样,只是工匠的正常行为。

您已安排该命令,因此它将在Laravels内部调度程序驱动的指定时间内执行(请参阅下面的cronjob)。但您也可以通过在CLI中输入命令来手动执行命令。

  

注意:手动执行命令与artisan的调度程序无关。只要您认为合适,调度程序就会执行此命令。

所以你要找的命令是:

php artisan schedule:run

此命令在指定时间内循环注册命令,因此只有在时间准备好后才会运行命令。

为了确保Laravel在适当的时候执行命令,创建一个cronjob(在您的服务器上)以每分钟运行php artisan schedule:run命令 ,Laravel负责其余的工作。

从文档(在crontab中插入):

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

古德勒克!

相关问题