<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                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`,大家可以關注一下。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看