php execute a background process

前端 未结 19 1349
萌比男神i
萌比男神i 2020-11-21 07:04

I need to execute a directory copy upon a user action, but the directories are quite large, so I would like to be able to perform such an action without the user being aware

相关标签:
19条回答
  • 2020-11-21 07:29

    If you need to just do something in background without the PHP page waiting for it to complete, you could use another (background) PHP script that is "invoked" with wget command. This background PHP script will be executed with privileges, of course, as any other PHP script on your system.

    Here is an example on Windows using wget from gnuwin32 packages.

    The background code (file test-proc-bg.php) as an exmple ...

    sleep(5);   // some delay
    file_put_contents('test.txt', date('Y-m-d/H:i:s.u')); // writes time in a file
    

    The foreground script, the one invoking ...

    $proc_command = "wget.exe http://localhost/test-proc-bg.php -q -O - -b";
    $proc = popen($proc_command, "r");
    pclose($proc);
    

    You must use the popen/pclose for this to work properly.

    The wget options:

    -q    keeps wget quiet.
    -O -  outputs to stdout.
    -b    works on background
    
    0 讨论(0)
  • 2020-11-21 07:29

    For those of us using Windows, look at this:

    Reference: http://php.net/manual/en/function.exec.php#43917

    I too wrestled with getting a program to run in the background in Windows while the script continues to execute. This method unlike the other solutions allows you to start any program minimized, maximized, or with no window at all. llbra@phpbrasil's solution does work but it sometimes produces an unwanted window on the desktop when you really want the task to run hidden.

    start Notepad.exe minimized in the background:

    <?php 
    $WshShell = new COM("WScript.Shell"); 
    $oExec = $WshShell->Run("notepad.exe", 7, false); 
    ?> 
    

    start a shell command invisible in the background:

    <?php 
    $WshShell = new COM("WScript.Shell"); 
    $oExec = $WshShell->Run("cmd /C dir /S %windir%", 0, false); 
    ?> 
    

    start MSPaint maximized and wait for you to close it before continuing the script:

    <?php 
    $WshShell = new COM("WScript.Shell"); 
    $oExec = $WshShell->Run("mspaint.exe", 3, true); 
    ?> 
    

    For more info on the Run() method go to: http://msdn.microsoft.com/library/en-us/script56/html/wsMthRun.asp

    Edited URL:

    Go to https://technet.microsoft.com/en-us/library/ee156605.aspx instead as the link above no longer exists.

    0 讨论(0)
  • 2020-11-21 07:31

    Can you arrange to fork off a separate process, and then run your copy in the background? It's been a while since I did any PHP, but the function pcntl-fork looks promising.

    0 讨论(0)
  • 2020-11-21 07:32

    You might want to try to append this to your command

    >/dev/null 2>/dev/null &
    

    eg.

    shell_exec('service named reload >/dev/null 2>/dev/null &');
    
    0 讨论(0)
  • 2020-11-21 07:32

    Thanks to this answer: A perfect tool to run a background process would be Symfony Process Component, which is based on proc_* functions, but it's much easier to use. See its documentation for more information.

    0 讨论(0)
  • 2020-11-21 07:33

    If you are looking to execute a background process via PHP, pipe the command's output to /dev/null and add & to the end of the command.

    exec("bg_process > /dev/null &");
    

    Note that you can not utilize the $output parameter of exec() or else PHP will hang (probably until the process completes).

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