在php函数中实现2个exec函数

在php函数中实现2个exec函数,php,Php,我想在php函数中实现2exec函数,比如 public function definition($source){ shell_exec(""); shell_exec(""); } public function definition($source){ shell_exec("cd C:\myfolder"); shell_exec(""); } enter code here public function de

我想在php函数中实现2exec函数,比如

public function definition($source){
     shell_exec("");
     shell_exec("");
}
    public function definition($source){
         shell_exec("cd C:\myfolder");
         shell_exec("");
    }
enter code here
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py myImage.jpg");
}
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py".$source);
}
现在我想按顺序实现它,首先执行一个,然后在第一个被执行之后,执行第二个。首先,我想在windows下实现cd(移动到文件夹),比如

public function definition($source){
     shell_exec("");
     shell_exec("");
}
    public function definition($source){
         shell_exec("cd C:\myfolder");
         shell_exec("");
    }
enter code here
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py myImage.jpg");
}
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py".$source);
}
在第二个示例中,我想用两个参数运行python脚本,第一个是python脚本,它位于我刚刚移动到的文件夹中,第二个是位于同一文件夹中的图像,类似于

public function definition($source){
     shell_exec("");
     shell_exec("");
}
    public function definition($source){
         shell_exec("cd C:\myfolder");
         shell_exec("");
    }
enter code here
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py myImage.jpg");
}
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py".$source);
}
现在我想把参数从函数定义传递到第二个python参数

public function definition($source){
     shell_exec("");
     shell_exec("");
}
    public function definition($source){
         shell_exec("cd C:\myfolder");
         shell_exec("");
    }
enter code here
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py myImage.jpg");
}
public function definition($source){
     shell_exec("cd C:\myfolder");
     shell_exec("python myScript.py".$source);
}

现在我想听听你对我需要的东西的解决方案,我已经尽了最大的努力,但没有解决它。感谢您的帮助和时间。

在Linux上,您可以这样做:

$cmd = 'command to run';
exec( 'bash -c "nohup $cmd > /dev/null 2>&1 &"' );
只有在exec命令完成后,此操作才会进行

我不知道您是否可以在Windows上执行此操作,但如果可以,最后的部分可能会是:

$cmd = 'command to run';
exec( 'bash -c "$cmd > nul 2>&1 &"' );
然而,我不确定“bash-c”的等价物是什么

可能的Windows解决方案:

$shell = new COM( "WScript.Shell" );
$shell->run( "your command", 0, false );
您也可以这样做:

public function definition( $source )
{
     chdir( 'C:\myfolder' );
     shell_exec( 'python myScript.py' . $source );
     # chdir back to your script directory if needed
}

我不确定PHP是否会使用该命令chdir到“C:\myfolder”。

您可以使用&>在windows上同时运行两个命令。因此,与其做两个shell_exec,不如做一个。它的形式是

shell_exec('CMD1 & CMD2');
我相信每次执行shell_exec时,都会打开一个新的shell,因此当您尝试运行python命令时,实际上是在错误的目录中

如果这不起作用,您也可以尝试使用两个&符号,而不是一个

shell_exec('CMD1 && CMD2');

希望这对你有用。

这不是我投的反对票。我已经给了你+1,尽管它对我没有多大帮助,但我至少试了一下/很抱歉,我不能提供更多帮助,因为我不使用Windows。第一个代码是我在Linux上使用的。我已经用这个版本更新了它,它可能在Windows上工作(猜测)。没有必要道歉!谢谢你的帮助!:)