gpt4 book ai didi

php - Symfony 进程组件中的多个命令

转载 作者:行者123 更新时间:2023-12-05 08:12:04 25 4
gpt4 key购买 nike

我正在尝试使用 Symfony 进程组件执行多个命令,但第二个命令未被处理。我做错了什么?

        $process = new Process('sshpass -p password ssh user@host');
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();

$process1 = new Process("sudo reboot -f");
$process1->run();
if (!$process1->isSuccessful()) {
throw new ProcessFailedException($process1);
}
echo $process1->getOutput();

最佳答案

这就是使用 symfony process 运行多个命令的方式

$process = new Process('sshpass -p password ssh user@host');
$process->run(); //Run the command (whole process)

if (!$process->isSuccessful()) { //Executes after the command finishes
throw new ProcessFailedException($process);
}

echo $process->getOutput(); //Output the result (optional)

$process->setCommandLine('sudo reboot -f'); //Set a new Command to the current process
$process->run(); //Run this process again

if (!$process->isSuccessful()) { //Executes after the command finishes
throw new ProcessFailedException($process);
}

echo $process->getOutput(); //Next output (also optional)

如评论中所述,setCommandLine 方法已在 symfony 5 中删除。因此此解决方案仅适用于 symfony <= 4.4(最新的 symfony 4 版本)

关于php - Symfony 进程组件中的多个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39588638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com