<?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;
//use think\Db;
use think\facade\Db;
class Message extends Command
{
protected function configure()
{
// 指令配置
$this->setName('message')
->setDescription('the message command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出(业务逻辑放这里)
$output->writeln('message');
}
}
在tp项目中可以运行php think Message 执行, 在shell配置如下
#!/bin/bash
# 设置ThinkPHP框架的根目录(确保这是您的项目根目录,而不是public目录)
APP_PATH=/home/textBook # 假设textBook是您的ThinkPHP项目名称
# 设置PHP的执行路径,如果php不在PATH中,需要指定完整路径
PHP_PATH=/opt/remi/php73/root/usr/bin/php
# 切换到ThinkPHP的根目录
cd "$APP_PATH" || exit
# 执行ThinkPHP的命令行任务
# 注意:这里不需要再指定$APP_PATH,因为我们已经切换到了项目根目录
# 也不需要在php think之前加上php,因为$PHP_PATH已经指定了PHP解释器
$PHP_PATH think Message
配置完shell后需要在linux中设置定时任务
在Linux系统中,可以使用cron来定期执行shell脚本。以下是设置定时任务的步骤:
- 打开终端。
- 输入 crontab -e 命令来编辑当前用户的cron任务。
- 在打开的编辑器中,添加一行表示你的定时任务。这行由5个字段和一个命令组成,格式如下:
分 时 日 月 周 命令 - 例如,要每天早上12点执行myTask.sh,你可以添加如下行:
0 12 * * * /bin/bash /home/textBook/public/myTask.sh - 保存并退出编辑器。cron将自动安装新的定时任务。
确保你的shell脚本有执行权限。如果没有,可以使用chmod命令来添加权限
如果碰到 /home/textBook/public/myTask.sh: line 2: $‘\r’: command not foundchmod +x /home/textBook/public/myTask.sh
这些错误通常表明你的myTask.sh脚本文件是在Windows环境中编辑的,并且包含了Windows风格的换行符(CR LF,即回车加换行\r\n),而Linux和Unix系统只识别换行符(LF,即\n)。此外,你的cd命令似乎指向了一个错误的目录,或者脚本中的路径有误。sed -i 's/\r$//' /home/textBook/public/myTask.sh
369

被折叠的 条评论
为什么被折叠?



