目录

宝塔-Linux-计划任务中添加运行项目网站PHP任务-定时任务

宝塔 Linux 计划任务中添加运行项目网站PHP任务-定时任务

一、指定php版运行,

cd /www/wwwroot/www.xxx.com/ && /www/server/php/56/bin/php think timedtasks start >> /tmp/timedtasks.log 2>&1

二、不指定php版

cd /www/wwwroot/www.xxx.com/ && php think timedtasks start >> /tmp/timedtasks.log 2>&1

# 使用绝对路径(通过 which php 获取)
cd /www/wwwroot/www.xxx.com/ && /usr/bin/php think timedtasks start >> /tmp/timedtasks.log 2>&1

切换工作目录 cd /www/wwwroot/www.xxx.com/ 确保 PHP 脚本在项目根目录下执行,避免因相对路径导致的文件找不到问题(常见于框架的入口文件、配置文件加载)。

指定 PHP 版本路径

/www/server/php/56/bin/php

直接使用绝对路径调用 PHP 5.6 的解释器,避免系统默认 PHP 版本冲突。

执行命令

think timedtasks start

假设这是你的项目自定义命令(如 ThinkPHP 框架的定时任务)。

日志记录(可选但强烈建议)

/tmp/timedtasks.log 2>&1

将标准输出和错误信息重定向到日志文件,便于后续排查问题

三、常见问题排查 3.1 权限问题 确保宝塔的 www 用户(或其他运行用户)有权限访问项目目录和 PHP 可执行文件: chown -R www:www /www/wwwroot/www.xxx.com/ chmod +x /www/server/php/56/bin/php 3.2 路径错误 手动在 SSH 中执行命令测试: /www/server/php/56/bin/php -v  # 确认 PHP 版本 cd /www/wwwroot/www.xxx.com/ && /www/server/php/56/bin/php think timedtasks start 3.3 依赖缺失 如果脚本依赖环境变量(如数据库配置),可在命令前通过 export 声明: export ENV=production && cd /path && php command