說說在Laravel中怎么執行Shell命令 ?

下面由laravel教程欄目帶大家介紹關于怎么在 laravel 中執行 shell 命令,希望對大家有所幫助!

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

說說在Laravel中怎么執行Shell命令 ?

shell_exec()?和?exec()?都可以執行 shell 命令。

如果你的命令不知道因為什么原因而崩潰,你將不會知道其原因 —— 因為shell_exec()?和?exec() 不會拋出異常,他們只是默默地執行失敗了。

這是我的解決方案:

use symfonyComponentProcessProcess;  class ShellCommand {     public static function execute($cmd): string     {         $process = Process::fromShellCommandline($cmd);          $processOutput = '';          $captureOutput = function ($type, $line) use (&$processOutput) {             $processOutput .= $line;         };          $process->setTimeout(null)             ->run($captureOutput);          if ($process->getExitCode()) {             $exception = new ShellCommandFailedException($cmd . " - " . $processOutput);             report($exception);              throw $exception;         }          return $processOutput;     } }
  • 它使用了 Symfony’s 的 Process 組件。?相關推薦:最新的五個laravel視頻教程

使用這種方法,我可以拋出一個自定義異常,記錄命令和輸出,或者是記錄到日志以尋找問題。

相關推薦:laravel


原文地址:https://dev.to/kodeas/executing-shell-commands-in-laravel-1098

譯文地址:https://learnku.com/laravel/t/63048

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享