一、任务创建

查看系统用户下的所有定时任务**

1
crontab -l

添加定时任务

1
crontab -e

i 进入编辑,添加一行

1
* * * * * cd /www/wwwroot/你的项目目录 && php think 你的命令 >> /tmp/tp_task.log 2>&1

示例(每分钟执行一次):

1
* * * * * cd /www/wwwroot/myproject && php think task:sync >> /tmp/tp_task.log 2>&1

你只需要改 2 个地方:

1
2
3
/www/wwwroot/你的项目目录
task:sync 换成你自己的命令
按 ESC,输入 :wq 保存退出。
1
2
3
4
5
6
7
8
9
10
*/5 * * * *
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── 星期 (0-7)
│ │ │ └────── 月份 (1-12)
│ │ └──────── 日期 (1-31)
│ └────────── 小时 (0-23)
└───────────── 分钟 (*/5 表示每5分钟)

2>&1 表示错误和正确一起输出到日志,每个都添加即可

二、定时任务维护

1.查看定时任务
1
crontab -l
2.查看定时任务日志
1
tail -f /var/log/cron
3.查看你命令的输出日志
1
tail -f /tmp/tp_task.log
4.查看执行用户,以下为www用户
1
crontab -u www -l

个人游戏开发

image-20250408192801332