Web應用在運行時會有執行時長的限制,但是實際開發中有些任務耗時比較長,甚至是常駐內存的,這時只能使用cli方式運行PHP腳本,也就是常說的命令行應用。
開發一個完整的命令行應用流程如下:
## 1、創建一個命令文件
~~~
$ php think make:command Push
Command:app\command\Push created successfully.
~~~
成功創建后,會生成一個文件:
~~~
<?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;
class Push extends Command
{
protected function configure()
{
// 指令配置
$this->setName('app\command\push')
->setDescription('the app\command\push command');
}
protected function execute(Input $input, Output $output)
{
// 指令輸出
$output->writeln('app\command\push');
}
}
~~~
## 2、添加功能
其中的 execute 方法,就是你要補充的。我給出一個我在項目中不完整的例子,如下。
~~~
protected function execute(Input $input, Output $output)
{
/* 讀入數據推送規則 */
$target = $input->getArgument('target');
$this->rules = Config::get('push_' . $target);
$output->writeln('Push ver: ' . self::$version . PHP_EOL);
if (!$this->rules) {
$output->writeln('Push rules not found.');
}
/* 遍歷規則 */
foreach ($this->rules as $rule) {
/* 啟用的規則 */
if ($rule['enable'] ?? false) {
/* 是否輸出調試信息和調試數據 */
$rule['debug'] = $rule['debug'] ?? false;
/* 獲取數據 */
$data = $this->getData($rule);
/* 寫到目標表中 */
if (isset($rule['target'])) {
$result = $this->setData($data, $rule);
$output->writeln('成功數: ' . $result[0] . ', 失敗數: ' . $result[1] . ',' . $rule['name'] .
PHP_EOL);
}
}
} //endforeach
}
~~~
## 3、配置命令
修改`config/console.php`文件,內容如下。
~~~
<?php
// +------------
// | 控制臺配置
// +------------
return [
// 指令定義
'commands' => [
'push' => 'app\command\Push',
],
];
~~~
## 4、運行命令
~~~
php think push
~~~
也可以用 Shell 包裝以下, 讓代碼運行更健壯.
~~~
#!/usr/bin/env bash
# 文件鎖
LOCK_FILE="/var/run/push_gonggai.pid"
# 日志文件
LOG_FILE="/var/log/push_gonggai.log"
# 需要 root 權限
if [ "$UID" -ne "0" ]; then
echo "Must be root to run this srcript."
exit 103
fi
if (set -o noclobber; echo "$$" > "$LOCK_FILE") 2> /dev/null; then
trap 'rm -f "$LOCK_FILE"; exit $?' INT TERM EXIT
echo "" >> ${LOG_FILE}
echo "=== BEGIN-TO-PUSH ===" >> ${LOG_FILE}
echo `date '+%Y-%m-%d %H:%M:%S'` >> ${LOG_FILE}
/usr/local/php/bin/php /home/www/gonggaixitong/think push>> ${LOG_FILE}
# 記錄結束時間
echo `date '+%Y-%m-%d %H:%M:%S'` >> ${LOG_FILE}
echo "=== END-OF-PUSH ===" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
rm -f $LOCK_FILE
trap - INT TERM EXIT
else
if [ ! -f $LOCK_FILE ]; then
echo "You have no right to excute the script."
exit 102
else
echo "The script is running, lock file is $LOCK_FILE."
exit 101
fi
fi
~~~
命令行程序非常簡單,腳本一旦出現錯誤,進程就會終止,所以需要配合進程監控工具才能使用到生產環境。本人常用的進程監控工具`pm2`及`supervisor`,大家可以關注一下。
- 搭建ThinkPHP6的開發環境
- 配置ThinkPHP6
- 必要的基礎知識(basic)
- MVC開發模式
- 控制器(controller)
- 數據庫(database)
- 模型(model)
- 模型關聯(relation)
- 視圖(view)
- Session
- Cookie
- 緩存(cache)
- 上傳(upload)
- 驗證器(validate)
- 驗證碼(captcha)
- 命令行(command)
- 服務器部署(deploy)
- 數據備份(backup)
- 數據同步(synchronization)
- 訂閱服務(subscribe)
- PHP 易混淆知識點
- 助手函數
- MySQL規范
- Redis 規范
- office插件 phpoffice
- 拼音插件 pinyin
- 日期插件 datetime
- 消息插件 amqp
- 產品部署環境的搭建
- PDF 等雜項處理
- 文件上傳
- 常用擴展
- flc/dysms
- 使用示例 ①
- 使用示例 ②
- qiniu/php-sdk
- 簡介
- 使用示例
- 使用示例 2 ②
- liliuwei/thinkphp-jump
- 擴展介紹
- 下載擴展
- 使用方法
- topthink/think-captcha
- 安裝擴展
- 驗證碼顯示
- 更換驗證碼
- 驗證碼校驗
- 驗證碼配置
- 自定義驗證碼
- phpoffice/phpspreadsheet
- 數據寫入表格
- 讀取表格數據
- topthink/think-queue
- 安裝
- 自定義函數
- 任務類
- 帶有日志的任務類