Home >PHP Framework >Laravel >Detailed explanation of how to use laravel's task scheduling (scheduled execution of tasks)
The following column Laravel Tutorial will introduce you to task scheduling (regular execution of tasks) using laravel. I hope it will be helpful to friends in need!
There is a very powerful function in laravel. You only need to add a cron entry on the server to execute all laravel tasks regularly.
Now we have the following data table:
I want the value of the cron field in the cron table to increase by 1 every minute, then I need the following steps:
1. Write laravel code in App\Console\Kernel.php
protected function schedule(Schedule $schedule) { $schedule->call(function () { DB::table('cron')->increment('cron'); })->everyMinute(); }
2. In the / of the service Add code to the var/spool/cron/root file
Note: It is best to use the vim editor to edit the file here. If you use winscp to edit the file, something will appear. Task execution issues.
Using vim tutorial link: http://www.cnblogs.com/zzdylan/p/5941706.html
Enter
crontab -e
on the command line and add the following code
* * * * * /usr/local/php/bin/php /data/wwwroot/test/artisan schedule:run 1>> /dev/null 2>&1
Enter
crontab -u root -l
on the command line. There is no need to restart the cron service, because the system will read /var/spool/cron every minute. Files in the directory.
If you find that it still cannot be executed according to the following configuration, you can use the following methods to troubleshoot the problem:
Check whether the command uses an absolute path, such as here /usr/local/php/bin/php instead of php, use /data/wwwroot/test/artisan instead of artisan .
If the absolute path is still not executed, then directly enter /usr/local/php/bin/php /data/wwwroot/test/artisan schedule:run 1>> on the command line ; /dev/null 2>&1 and see if it is executed. If it is not executed, it is a problem with the laravel code. If it is executed, it means that it is an environment variable problem. Check the path. If you don’t know where php is, enter which php on the command line, and you will be prompted where php is installed.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of Detailed explanation of how to use laravel's task scheduling (scheduled execution of tasks). For more information, please follow other related articles on the PHP Chinese website!