在PHP中执行bash if else命令(bash与php)

时间:2018-09-04 10:31:15

标签: php bash shell exec

我想实现的目标 我想执行一些脚本,该脚本的进程未在服务器上启动。因此,我正在用shell脚本准备命令并在一行中执行它。

带有php变量的命令

$cmd = "if [[ `ps auxww | grep -v grep | grep ".$process_file." | grep '".$find."'`  == '' ]] ; then ".$cmd2."  fi";

echo $cmd."\n";

已执行的命令,一旦替换了变量(将在bash上实际运行):

if [[ `ps auxww | grep -v grep | grep /home/new_jig.php | grep 'test_51 1714052'`  == '' ]] ; then  php /home/new_jig.php test_51 1714052 & fi;

执行命令

exec($cmd,$out,$res);

请注意,我还将问题分成两个语句并执行了。但这很耗时。当列表中有2000个以上并且所有命令都执行时,这会引起问题。大约需要1分钟或1分钟以上才能到达最后一个数字。

我想在10秒内实现这一目标。请帮助我达到最佳输出。

谢谢 吉涅什

1 个答案:

答案 0 :(得分:0)

我可以通过以下命令使其执行

$ process_file =执行某些功能的phpfile

$ cmd2 =“ if(accountName.equalsIgnoreCase(y.getName())) { ”;

$ cmd11 =“ if php ".$process_file." 1212 >/dev/null 2>/dev/null &;然后回显1;”。$ cmd2。“; fi”;

shell_exec($ cmd11。“> / dev / null 2> / dev / null&”);

在此之前:对于1100个请求,该过程大约需要 60秒钟以上 之后: 20到30秒