在单个exec PHP语句中执行两个shell命令


execute two shell commands in single exec php statement

我想显示在指定日期之后修改的所有文件

命令是

touch --date '2011-09-19 /home/  , find /home/

我如何在一个exec语句中执行这两个命令。提前感谢

可以使用;&&分隔命令。;将无条件执行这两个命令。如果第一个程序失败了,第二个程序仍然运行。使用&&使第二个命令依赖于第一个命令。如果第一个命令失败,第二个命令将不运行。

command1 ; command2     (run both uncondtionally)
command1 && command2     (run command2 only if command1 succeeds)

这就是我如何同时编码拇指,然后flv视频..我需要从avi文件生成两个拇指。在拇指之后,我需要将相同的avi转换为flv或其他格式。所以,这是我通常使用的代码。

$command_one = "do whatever the script does best";
$command_two = "do whatever the script does second best";
//execute and redirect standard stream ..
@exec($command_one."&& ".$command_two.">/dev/null 1>/dev/null 2>/dev/null &");

如果你愿意,你也可以用exec来运行命令数组:)

foreach($crapatoids as $crap){
$command_name = $crap;
//exec the crap below
@exec ($command_name." >/dev/null 1>/dev/null 2>/dev/null &");
}

用分号分隔。例子:

exec("touch --date '2011-09-19' /home/; find /home/");

分号分隔符允许在一行上运行多个命令。

<?php
    $output = shell_exec("touch --date '2011-09-19' /home/; find /home/");
    echo "<pre>" . $output . "</pre>";
?>

实际上,我的问题来自于在python的虚拟环境中执行python文件。一般来说,Python网站指导我们通过命令行:创建一个虚拟环境-->激活它-->调用Python文件(例如:python3 yourPyFile.py)。然而,当我试图通过在php中调用exec()方法来适应这些步骤时,它不起作用。最后,我发现你根本不需要激活env,你只需要使用path/to/virtual/env/bin/python3 yourPyFile.py创建虚拟env时已经生成的python。