说说在Laravel中怎么执行Shell命令 ?
时间:2021-12-04 来源:互联网
今天PHP爱好者给大家带来下面由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;
}
}
它使用了 Symfony's 的 Process 组件。 相关以上就是说说在Laravel中怎么执行Shell命令 ?的详细内容,更多请关注php爱好者其它相关文章!
-
Anaconda是干嘛用的 Anaconda介绍、安装及使用教程 2025-09-17
-
百度SEO优化标题:2024最新电视剧免费下载方法 高清资源一键获取教程 2025-09-17
-
Swagger是什么工具 Swagger工具详细使用介绍 2025-09-17
-
2023最新单机游戏下载教程 安全高速资源一键获取指南 2025-09-17
-
剃刀是什么梗?揭秘网络热梗背后的幽默哲学,3秒get笑点! 2025-09-17
-
Curl error: SSL connection timeout 2025-09-17