Home >PHP Framework >Laravel >Do laravel scheduled tasks need to use cron?
Laravel scheduled tasks need to use cron; if you want to execute all laravel tasks regularly in laravel, you need to add cron entries on the server and add "phppath project path/artisan schedule:run..." to the root file. "The code is enough.
The operating environment of this article: linux7.3 system, Laravel version 5.4, Dell G3 computer.
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 to be as follows 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. Add code to the /var/spool/cron/root file of the service
Note: It is best to use the vim editor to edit the file here. If you use winscp to edit the file, there will be a problem of not executing the task.
Enter on the command line
crontab -e
Add the following code
* * * * * /usr/local/php/bin/php /data/wwwroot/test/artisan schedule:run 1>> /dev/null 2>&1
Enter on the command line
crontab -u root -l##No need to restart cron service, because the system reads the files in the /var/spool/cron directory every minute. 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, for example, use /usr/local/php/bin/ here 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>> /dev on the command line /null 2>&1, 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 problem. If you don't know where php is, enter which php on the command line, you will be prompted where php is installed. [Related recommendations:
laravel video tutorial]
The above is the detailed content of Do laravel scheduled tasks need to use cron?. For more information, please follow other related articles on the PHP Chinese website!