thinkphp executes the scheduled task crontab

thinkphp executes the scheduled task crontab

The implementation of php scheduled tasks in tp5.1 can essentially be divided into two types. The first is encapsulated in the framework. Just add one in crontab -e, and then expand the content in the framework (Laravel’sschedule , task Scheduling), this degree of freedom is relatively high, and it is also relatively convenient. The second: create a PHP execution script, execute the script in crontab, or write another .sh to execute in crontab, this kind of needs to execute some files on the server, relatively speaking, it is more troublesome, of course the effect is still the same of.

/usr/bin/php/data/wwwroot/default/1.php

Finally, in crontab, execute it once a minute, output to log, and check the effect. The simple self-test method is directly in the directory where test.sh is located./test.sh or sh test.sh, you can view the script running results

*/1 * * * * sh/data/wwwroot/default/test.sh 2 >>/data/wwwroot/default/log.txt  

Method 2. Use the think custom command function that comes with tp, create a command folder/Crontab.class.php in application/modules/, the class can be omitted, if class_suffix is ​​turned off in the configured app.php, modify The file name is Crontab.php

namespace appapicommand;
use thinkconsoleCommand;

use thinkconsoleOutput;

class Crontab extends Command

protected function configure(){
$this->setName('Crontab')->setDescription("Scheduled Task Crontab");


protected function execute(Input $input, Output $output){
$output->writeln('Date Crontab job start...');





$output->writeln('Date Crontab job end...');

Configure the command.php file, the location is application/command.php

return ['appapicommandCrontab'];

Open the command line and run the php think Test command, the method running in the execute method of the test command will run. The rest is to write a Crontab.sh to execute, pay attention to the format

cd/data/wwwroot/default/purenfort-api
/usr/bin/php think Crontab

Finally, in crontab, execute it once a minute and output to the log. The effect can be checked. The simple self-test method is directly in the directory where Crontab.sh is located./Crontab.sh or sh Crontab.sh can view the script running It turned out. Note that php should write the full path in the shell script, and pay attention to the permissions and encoding format of the .sh script. Nothing else.

Reference: https://cloud.tencent.com/developer/article/1531576 thinkphp executes the scheduled task crontab-Cloud + Community-Tencent Cloud