1.配置文件:application/command.php
return [
'web\common\command\Console',
];
2.创建自定义的命令行:在home模块中的command创建一个Console.php文件(注意大小写)
<?php
/**
* Created by PhpStorm.
* User: SUN
* Date: 2018/9/11
* Time: 11:21
*/
namespace web\common\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Console extends Command
{
protected function configure()
{
$this->setName('console')->setDescription('scheduled task');
}
protected function execute(Input $input, Output $output)
{
$output->write('begin scheduled task:');
$productM = new \web\mobile\controller\Product();
$productM->teamIncome();
}
}
3.在项目根目录下新建console.sh文件,并赋予777权限
微信截图_20200506151730.png
#!/bin/sh
cd /mnt/www/course
php think console
4.测试:执行.sh文件
./console.sh
5.在定时器中执行:crontab -e
0 2 * * * /mnt/www/course/console.sh
* * * * * /usr/bin/curl http://www.test.com
6.命令解释
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
7.crontab最小执行时间为分钟,要实现秒级,有一下2种方法
第一种:
crontab 的延时,通过延时方法 sleep N 来实现没N秒执行
* * * * * curl http://test.com/api/sale/robotBid
* * * * * sleep 10; curl http://test.com/api/sale/robotBid
* * * * * sleep 20; curl http://test.com/api/sale/robotBid
* * * * * sleep 30; curl http://test.com/api/sale/robotBid
* * * * * sleep 40; curl http://test.com/api/sale/robotBid
* * * * * sleep 50; curl http://test.com/api/sale/robotBid
注意:60必须能整除间隔的秒数(没用余数),例如:2,5,10等。
第二种:
shell 脚本实现,写一个后台运行的脚本一直循环,每次循环sleep一段时间
可能创建多个线程,导致cpu飙升,请注意使用
#!/bin/bash
while true;
do
curl http://test.com/api/sale/robotBid
sleep 5
done