dsdtszi0520538
2010-11-18 11:00 阅读 39
已采纳

PHP和shell_exec

I have a PHP website and I would like to execute a very long Python script in background (300 MB memory and 100 seconds). The process communication is done via database: when the Python script finishes its job, it updates a field in database and then the website renders some graphics, based on the results of the Python script.

I can execute "manually" the Python script from bash (any current directory) and it works. I would like to integrate it in PHP and I tried the function shell_exec:

shell_exec("python /full/path/to/my/script") but it's not working (I don't see any output)

Do you have any ideas or suggestions? It worths to mention that the python script is a wrapper over other polyglot tools (Java mixed with C++).

Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    dongqu9917 dongqu9917 2010-11-20 09:50

    Thanks for your answers, but none of them worked :(. I decided to implement in a dirty way, using busy waiting, instead of triggering an event when a record is inserted.

    I wrote a backup process that runs forever and at each iteration checks if there is something new in database. When it finds a record, it executes the script and everything is fine. The idea is that I launch the backup process from the shell.

    点赞 评论 复制链接分享
  • duanjin9035 duanjin9035 2010-11-18 11:06

    A proplem could be that your script takes longer than the server waiting time definied for a request (can be set in the php.ini or httpd.conf).

    Another issue could be that the servers account does not have the right to execute or access code or files needed for your script to run.

    点赞 评论 复制链接分享
  • douzongluo7542 douzongluo7542 2010-11-18 14:35

    First off set_time_limit(0); will make your script run for ever so timeout shouldn't be an issue. Second any *exec call in PHP does NOT use the PATH by default (might depend on configuration), so your script will exit without giving any info on the problem, and it quite often ends up being that it can't find the program, in this case python. So change it to:

    shell_exec("/full/path/to/python /full/path/to/my/script");
    

    If your python script is running on it's own without problems, then it's very likely this is the problem. As for the memory, I'm pretty sure PHP won't use the same memory python is using. So if it's using 300MB PHP should stay at default (say 1MB) and just wait for the end of shell_exec.

    点赞 评论 复制链接分享
  • download1323 download1323 2010-11-18 15:25

    Found this before and helped me solve my background execution problem:

    function background_exec($command)
    {
        if(substr(php_uname(), 0, 7) == 'Windows')
        {
            pclose(popen('start "background_exec" ' . $command, 'r'));
        }
        else
        {
            exec($command . ' > /dev/null &');
        }
    }
    

    Source:

    http://www.warpturn.com/execute-a-background-process-on-windows-and-linux-with-php/

    点赞 评论 复制链接分享
  • dongqu9972 dongqu9972 2012-10-22 23:47

    shell_exec returns a string, if you run it alone it won't produce any output, so you can write:

    $output = shell_exec(...);
    print $output;
    
    点赞 评论 复制链接分享
  • dongmanzui8486 dongmanzui8486 2015-01-07 15:28

    I found that the issue when I tried this was the simple fact that I did not compile the source on the server I was running it on. By compiling on your local machine and then uploading to your server, it will be corrupted in some way. shell_exec() should work by compiling the source you are trying to run on the same server your are running the script.

    点赞 评论 复制链接分享

相关推荐