douchi5822 2014-07-20 03:47
浏览 96
已采纳

从PHP脚本在后台使用&&执行多个linux命令

Ok so I have a operating system Ubuntu. I have lampp. Now I wanna execute some command in the background which is going to take atleast 10-15 minutes to execute. I want to execute this command from PHP script from web interface not cli.

First Instance:

When I do this , the php script is successfully able to run the command in the background:

command1 &> /dev/null &

Second Instance:

But when I do this:

command1 &> /dev/null && command2 &

,

Then the command does not run in background, the php script pauses until this command executes. I want the command2 to execute after command1 is completed so that I (or my php script) can know that command1 has been executed. But it should be in background else my php script doesn't execute on time.

When I do the second instance from command line, it is able to run in background, but when I do it from php script then it is unable to run in background.

I am using the exec function of php to execute those commands.

<?php
exec('command1 &> /dev/null && command2 &',$out,$ret);
?>

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dongshui2254 2014-07-20 04:08
    关注

    Try this:

    <?php
    exec('(command1 && command2) >/dev/null &',$out,$ret);
    

    What it's doing is launching your commands in a subshell so that command1 runs first and then command2 only runs after command1 completes successfully, then redirects all the output to dev/null and runs the whole thing in the background.

    If you want to run command2 regardless of the exit code of command1 use ; instead of &&.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能