54 lines
1.5 KiB
Markdown
54 lines
1.5 KiB
Markdown
# 计划任务
|
|
|
|
## 特别说明
|
|
|
|
本项目使用的 是 `yunwuxin/think-cron` 包,该包提供了丰富的功能,包括任务调度、任务执行、任务状态管理等。该包支持多种任务类型,如固定任务、循环任务、一次性任务等,同时支持多种执行周期,如分钟、小时、天、周、月等。
|
|
|
|
## 手动启动计划任务
|
|
|
|
``` bash
|
|
############ 查看 cron 进程
|
|
## 查看 cron 进程
|
|
ps aux | grep "think cron:schedule"
|
|
# # 精确查找
|
|
pgrep -f "think cron:schedule"
|
|
# # 查看进程树
|
|
pstree -p | grep -i cron
|
|
|
|
## 启动 cron 任务
|
|
# 守护进程模式
|
|
nohup php think cron:schedule > /dev/null 2>&1 &
|
|
#######################################
|
|
```
|
|
|
|
|
|
## 计划任务管理
|
|
|
|
``` php
|
|
/**
|
|
* 添加计划任务
|
|
* @param int $type 任务类型 1.固定任务 2.循环任务
|
|
* @param int $period 执行周期
|
|
* @param string $name 任务名称
|
|
* @param string $event 执行事件
|
|
* @param int $execute_time 待执行时间
|
|
* @param int $relate_id 关联id
|
|
* @param int $period_type 周期类型 0 分钟 1 天 2 周 3 月
|
|
*/
|
|
public function addCron($type, $period, $name, $event, $execute_time, $relate_id, $period_type = 0)
|
|
{
|
|
$data = [
|
|
'type' => $type,
|
|
'period' => $period,
|
|
'period_type' => $period_type,
|
|
'name' => $name,
|
|
'event' => $event,
|
|
'execute_time' => $execute_time,
|
|
'relate_id' => $relate_id,
|
|
'create_time' => time()
|
|
];
|
|
$res = model('cron')->add($data);
|
|
return $this->success($res);
|
|
}
|
|
```
|