douzi8548 2011-08-10 14:48
浏览 96
已采纳

多个exec命令php [复制]

Possible Duplicate:
php in background exec() function

I am trying to run two exec commands from PHP. One is a script that might run for a few minutes and there can only be once instance of it. The second exec just checks if the first process is running and either lets you run it again or redirects you. The code is something like this.

This is the main process

$command = '/home/user/active/Nem-Swd.elf 30 > /dev/null 2>&1 & echo $!';

This is the process that checks if the other one is running already.

exec("ps ax | grep $name 2>&1", $output);

I know that in the php API it says :

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

But I don't understand what that implies.

  • 写回答

1条回答 默认 最新

  • dongmao4486 2011-08-10 14:59
    关注

    It means that if you run the exec command without re-routing its output, your current PHP script will wait for that command to finish before terminating the script. Since you are routing the output to standard out, I believe you should be ok. If you just ran exec($commandJob) you then would run into this problem.


    Seems that this question has already been answered:
    php in background exec() function

    At a minimum (for example) you need to do this to make it run in the background:
    exec("php test.php &");


    There are other ways to start processes that might be worth looking into as well: http://www.php.net/manual/en/book.pcntl.php
    http://php.net/manual/en/function.popen.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?