This article mainly introduces the Laravel implementation of the Timing task sample code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
Brief introduction
Timed tasks are a very common requirement in the backend development process, which often occurs in data statistics, garbage cleanup and other scenarios. Laravel provides a complete set of timing task tools, so that we only need to focus on the logic, the rest of the basic work will be borne by it.
Basic usage
Generate commands
PHP Artisan Make:command Areyouok
5.2 and previous versions, this command is ' PHP artisan make:console xxx '
Edit command
To edit the ' app/console/commands/areyouok.php ' file, modify the following:
... protected $signature = ' Areyou:ok '; Command name protected $description = ' Lei June, the most singing man in the Tech circle '; Command description, nothing with public function __construct () { parent::__construct (); The initialization code is written here, and it's nothing.}public function handle () { //function code written here}
Registration command
Edit the ' app/console/kernel.php ' file to register the newly generated class:
protected $commands = [ \app\console\commands\areyouok::class,];
Write the invocation logic:
protected function Schedule (schedule $schedule) { $schedule->command (' Areyou:ok ') ->timezone (' asia/ Shanghai ') ->everyminute ();}
The logic above is called once per minute. Laravel provides a time function of various lengths from one minute to one year, which can be called directly.
Register this Laravel project in the system cron.
To edit the '/etc/crontab ' file, add the following code:
* * * * * * Root/usr/bin/php/var/www/xxxlaravel/artisan schedule:run >>/dev/null 2>&1
The '/var/www/xxxlaravel ' in the line above needs to be changed to the actual path.
Kindle
Restart Cron to activate this feature: ' Systemctl restart Crond.service ', done!
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!