PHP and shell_exec

前端 未结 6 1113
余生分开走
余生分开走 2021-01-22 14:06

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 th

相关标签:
6条回答
  • 2021-01-22 14:52

    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.

    0 讨论(0)
  • 2021-01-22 14:54

    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.

    0 讨论(0)
  • 2021-01-22 14:54

    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/

    0 讨论(0)
  • 2021-01-22 14:58

    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;
    
    0 讨论(0)
  • 2021-01-22 14:58

    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.

    0 讨论(0)
  • 2021-01-22 15:05

    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.

    0 讨论(0)
提交回复
热议问题