php execute a background process

前端 未结 19 1347
萌比男神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:21

    Write the process as a server-side script in whatever language (php/bash/perl/etc) is handy and then call it from the process control functions in your php script.

    The function probably detects if standard io is used as the output stream and if it is then that will set the return value..if not then it ends

    proc_close( proc_open( "./command --foo=1 &", array(), $foo ) );
    

    I tested this quickly from the command line using "sleep 25s" as the command and it worked like a charm.

    (Answer found here)

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

    If using PHP there is a much easier way to do this using pcntl_fork:

    http://www.php.net/manual/en/function.pcntl-fork.php

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

    Well i found a bit faster and easier version to use

    shell_exec('screen -dmS $name_of_screen $command'); 
    

    and it works.

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

    You might try a queuing system like Resque. You then can generate a job, that processes the information and quite fast return with the "processing" image. With this approach you won't know when it is finished though.

    This solution is intended for larger scale applications, where you don't want your front machines to do the heavy lifting, so they can process user requests. Therefore it might or might not work with physical data like files and folders, but for processing more complicated logic or other asynchronous tasks (ie new registrations mails) it is nice to have and very scalable.

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

    I know it is a 100 year old post, but anyway, thought it might be useful to someone. You can put an invisible image somewhere on the page pointing to the url that needs to run in the background, like this:

    <img src="run-in-background.php" border="0" alt="" width="1" height="1" />

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

    PHP scripting is not like other desktop application developing language. In desktop application languages we can set daemon threads to run a background process but in PHP a process is occuring when user request for a page. However It is possible to set a background job using server's cron job functionality which php script runs.

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