1、command.php 配置
<?php
return [
'app\command\demoCommand',
];
2、demoCommand.php
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class demoCommand extends Command
{
protected function configure()
{
// 指令配置
$this->setName('demo:test');
// 设置参数
$this->setDescription('描述');
}
protected function execute(Input $input, Output $output)
{
// 实际业务
}
}
3、执行任务
*注:执行命令一定要跟上面定义的一致
php think demo:test
4、crontab 执行任务
*注:执行命令一定要跟上面定义的一致
*注:/usr/bin/php php安装位置自行修改(find / -name php
)
*注:/www/wwwroot/项目名/think 项目位置自行修改(find / -name php
)
*/1 * * * * /usr/bin/php /www/wwwroot/项目名/think demo:test
创建、编辑计划任务:
crontab -e
查看当前计划任务:
crontab -l
删除某条计划任务:
crontab -r
说明( * 占位):
minute hour day month week command 顺序:分 时 日 月 周 命令
使用crond设置任务的参数字段说明:
minute 表示分钟,是从0到59之间的任何整数
hour 表示小时,是从0到23之间的任何整数
day 表示日期,是从1到31之间的任何整数
month 表示月份,是从1到12之间的任何整数
week 表示星期,是从0到7之间的任何整数,其中0或7代表星期日
command 要执行的命令,可以是系统命令,也可以是自己编写的脚本文件
查看 crontab 状态:
service crond status
启动服务:
service crond start/
关闭服务:
service crond stop
重启服务:
service crond restart
重新载入配置:
service crond reload