How do I close a connection early?

前端 未结 19 1436
情书的邮戳
情书的邮戳 2020-11-22 04:24

I\'m attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I\'d like the script to simply send a response indicating that the process has star

19条回答
  •  灰色年华
    2020-11-22 05:03

    Assuming you have a Linux server and root access, try this. It is the simplest solution I have found.

    Create a new directory for the following files and give it full permissions. (We can make it more secure later.)

    mkdir test
    chmod -R 777 test
    cd test
    

    Put this in a file called bgping.

    echo starting bgping
    ping -c 15 www.google.com > dump.txt &
    echo ending bgping
    

    Note the &. The ping command will run in the background while the current process moves on to the echo command. It will ping www.google.com 15 times, which will take about 15 seconds.

    Make it executable.

    chmod 777 bgping
    

    Put this in a file called bgtest.php.

    
    

    When you request bgtest.php in your browser, you should get the following response quickly, without waiting about 15 seconds for the ping command to complete.

    start bgtest.php
    output:Array
    (
        [0] => starting bgping
        [1] => ending bgping
    )
    
    result:0
    end bgtest.php
    

    The ping command should now be running on the server. Instead of the ping command, you could run a PHP script:

    php -n -f largejob.php > dump.txt &
    

    Hope this helps!

提交回复
热议问题