学习目标:
- 配置thinkphp中自定义指令
- 配置linux中crontab
学习内容:
例如:
- 创建自定义指令
- linux配置定时任务
学习产出:
创作环境说明,thinkphp做了合同管理,其中有合同期限,合同到期需要将订单状态给为已完成,这里就需要每天对合同进行一次循环判断合同状态是否变更
thinkphp创建自定义指令
首先,创建一个李自定义命令类文件,根据自身需求变更
php think make:command ChangeStatus chanagestatus
命令会在app/command/
生成ChangeStatus.php
文件
<?php
declare (strict_types = 1);
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Hello extends Command
{
protected function configure()
{
// 指令配置
$this->setName('changestatus')
->setDescription('the changestatus command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('changestatus start');
$output->writeln('changestatus end');
}
}
configure()
配置无关紧要,根据自己情况修改或不动都是可以的
execute()
就是我们这运行自定义命令时执行的操作
其次,我们打开config/console.php
文件配置
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'changestatus'=>app\command\ChangeStatus::class, //根据自身情况,变更数据
],
];
最后,我们运行php think list
就可以看到我们自定义的命令了
> php think list
version 6.1.4
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-V, --version Display this console version
-q, --quiet Do not output any message
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
changestatus the changestatus command //在这里
>php think changestatus //执行看一下效果
changestatus start
changestatus end
>
恭喜你,万里长征第一步完成
配置linux定时任务
首先,你要先了解crontab,查看Linux系统是否安装crontabs
,安装我们就不过多赘述了。
我们看一下是否正常运行执行systemctl status crond
(服务对应的进程名字为crond)。
其次,我们编写好我们需要用到命令,这里我将其写入了shell脚本中
存储地址根据需要变更,首行必须写#! /bin/bash
,想不明白你就想想php文件怎么写,一个意思用来解释你编写的脚本的。
- 创建Shell脚本
[root@iZbp18brdnpqb20dqp8093Z ~]# vim /app/changestatus.sh
- 添加命令
#!/bin/bash
/opt/remi/php74/root/usr/bin/php /var/www/thinkphp/think changestatus
- 赋予脚本执行权限
[root@iZbp18brdnpqb20dqp8093Z ~]# chmod +x changestatus.sh
最后,了解一下crontab的操作命令和时间格式
//操作命令
crontab -l //查看当前用户的crontab文件内容
crontab -e //编辑当前用户的crontab文件内容
crontab -r //删除当前用户的crontab文件内容
//时间格式
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * user-name command to be executed
举例
*/5 * * * * command 每5分钟执行一次
0 * * * * command 每小时执行一次
0 0 * * * command 每天执行一次
0 0 * * 0 command 每周执行一次
0 0 1 0 0 command 每月执行一次
3,15 8-11 * * 1 command 每个星期一的上午8-11点的第3分钟和第15分钟执行
15 8-11 */2 * * command 每隔两天的上午8-11点的第15分钟执行
45 4 1,10,22 * * command 每月1、10、22日的4:45分执行
0 19 * * 1-5 command 每周一至周五晚上7点执行
注意:
- 整点分钟位置需要填
0
; - 连续的数字可以用
-
简写1-5; - 非连续的数字可以用
,
连接; - 每隔多长时间用
*/n
表示;
- 打开自定义任务列表
[root@iZbp18brdnpqb20dqp8093Z ~]# crcontab -e
- 写入自定任务 - 每天凌晨2点执行一次
0 2 * * * /bin/bash /app/changestatus.sh
搞定,下班。
新增FQA:
Q1:
A1: