500(内部服务器错误)php shell_exec

问题描述:

I am using php shell_exec to run a command from jQuery but it keeps timing out.

So my jquery is as follows.

var send = {
    url: $(this).data('url')
}

$.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>index.php/upload",
    data: send,
    //dataType: "json",
    //timeout: 8000,
    beforeSend: function() {

    },
    success: function(response) {
        console.log(response);
    },
    error: function () {
        alert('error');
    }
});

This then calls a function which runs shell_exec

function upload(){
    $output = shell_exec("wget {$_POST['url']} 2>&1");      
    echo "<pre>" . $output . "</pre>";
}

Ok this works fine if the url that is posted for wget get is pulling a small file but if wget is getting a file say over 20mb i get 500 (Internal Server Error)

Things i have checked so far all my php setting file upload limit max file size etc memory limit all i have set to the max.

The thing that i dont get is this worked fine locally but on my hosting i get this error, i have contact my hosting media temple and they have said it is out of their scope of support.

Any suggestion on how to debug further?

What is happening is that some kind of reverse proxy - for example Apache balancer - is timing out, whether the PHP script times out or not. You can verify by creating a script such as

<?php sleep(60); ?>

and experimenting with different delays.

Anyway, upping the time limit would only appear to be a solution, since some browsers (e.g. IE8+) will timeout no matter what.

Usually this is solved by detaching the wget process and having a PHP page checking on the process status with a refresh of, say, 5..30 seconds. But many providers take an understandably dim view of detaching processes, since this might hog the memory of a system serving dozens of websites (=paying customers!). So you'd have to check it out with the provider whether the solution is acceptable.

If it is, you can check out " Asynchronous shell exec in PHP ".