方式一:sh(最简洁暴力):
#!/bin/bash
// 1.执行thinkphp5下到index方法
/usr/bin/php /home/wwwroot/default/tp5/public/index.php /index/index/index
// 2.执行thinkphp5下的test方法
/usr/bin/php /home/wwwroot/default/tp5/public/index.php /index/index/test
// 执行普通文件
/usr/bin/php /root/learnScript/a.php
方式二:tp5
1.新建command文件
在application/模块/新建一个command文件夹/Test.class.php
<?php
namespace app\admin\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Test extends Command
{
protected function configure(){
$this->setName('Test')->setDescription("计划任务 Test");
}
protected function execute(Input $input, Output $output){
$output->writeln('Date Crontab job start...');
/*** 这里写计划任务列表集 START ***/
$this->test();
/*** 这里写计划任务列表集 END ***/
$output->writeln('Date Crontab job end...');
}
private function test(){
echo "test\r\n";
}
}
2.配置command.php文件,位置在application/command.php
<?php
return ['app\admin\command\Test'];
3.运行test命令
打开命令行,运行php think Test命令test命令execute方法中运行的方法就会运行
4.crontab定时执行
先在项目根目录创建一个crond.sh的执行文件,输入下面的内容(加上php程序的目录)
#!bin/bash
cd bash的目录
php think test
开放执行权限:chmond u+x crond.sh
然后运行下面的教程,配置计划任务:
命令行:crontab -e
然后再界面输入:*/1 * * * * bash 你的目录/crond.sh
保存退出
然后重启crond:
命令:service crond restart