下面由laravel教程栏目带大家介绍关于怎么在 laravel 中执行 shell 命令,希望对大家有所帮助!
shell_exec() 和 exec() 都可以执行 shell 命令。
如果你的命令不知道因为什么原因而崩溃,你将不会知道其原因 —— 因为shell_exec() 和 exec() 不会抛出异常,他们只是默默地执行失败了。
这是我的解决方案:
use Symfony\Component\Process\Process; 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; } }
使用这种方法,我可以抛出一个自定义异常,记录命令和输出,或者是记录到日志以寻找问题。
相关推荐:最新的五个Laravel视频教程
原文地址:https://dev.to/kodeas/executing-shell-commands-in-laravel-1098
译文地址:https://learnku.com/laravel/t/63048
以上就是说说在Laravel中怎么执行Shell命令 ?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号