Php run page in background
Problem Show During this time, the individual who posted the alert will sit on the last page of the form for almost a minute without any positive reinforcement that their notification is being posted. This leads to other potential problems, all that have possible solutions that I feel are less than ideal.
(Possible) Solution But, what if I can run this script as a background process? So, my question is this: How can I execute a PHP script to trigger as a background service and run completely independent of what the user has done at the form level? EDIT: This cannot be cron'ed. It must run the instant the form is submitted. These are high-priority notifications. In addition, the system administrators running our servers disallow crons from running any more frequently than 5 minutes. asked Jan 7, 2011 at 15:05
Michael IrigoyenMichael Irigoyen 22k17 gold badges85 silver badges131 bronze badges 5 Doing some experimentation with I'm using the following line to invoke the email script:
It is important to notice the The extra variables surrounded in single quotes after the path to the script are set as The email script then outputs to my log file using the
answered Jan 7, 2011 at 17:19
Michael IrigoyenMichael Irigoyen 22k17 gold badges85 silver badges131 bronze badges 6 On Linux/Unix servers, you can execute a job in the background by using proc_open:
The answered Jan 7, 2011 at 15:16
netcodernetcoder 64.9k17 gold badges120 silver badges142 bronze badges 3 Of all the answers, none considered the ridiculously easy fastcgi_finish_request function, that when called, flushes all remaining output to the browser and closes the Fastcgi session and the HTTP connection, while letting the script run in the background. Example:
Note: Due to a wontfix quirk in which calling flush() after fastcgi_finish_request will cause it to exit without warning/error. You may wish to call
Or
answered Jun 1, 2017 at 11:02
DanogentiliDanogentili 5976 silver badges13 bronze badges 1 PHP From the Manual:
So if you redirect the output to a log file (what is a good idea anyways), your calling script will not hang and your email script will run in bg. answered Jan 7, 2011 at 15:13
SimonSimon 4,2158 gold badges32 silver badges49 bronze badges 4 And why not making a HTTP Request on the script and ignoring the response ? http://php.net/manual/en/function.httprequest-send.php If you make your request on the script you need to call your webserver will run it in background and you can (in your main script) show a message telling the user that the script is running. answered Jan 7, 2011 at 15:13
TwisterTwister 7394 silver badges9 bronze badges The simpler way to run a PHP script in background is
The script will run in background and the page will also reach the action page faster.
LSerni 54.3k9 gold badges63 silver badges106 bronze badges answered Feb 7, 2013 at 12:45
1 How about this?
This second PHP script should be set to run as a cron. answered Jan 7, 2011 at 15:09
adarshradarshr 59.8k23 gold badges135 silver badges164 bronze badges 1 As I know you cannot do this in easy way (see fork exec etc (don't work under windows)), may be you can reverse the approach, use the background of the browser posting the form in ajax, so if the post still work you've no wait time. About sending mail it's always suggest to use a
spooler, may be a local & quick smtp server that accept your requests and the spool them to the real MTA or put all in a DB, than use a cron that spool the queue.
answered Jan 7, 2011 at 15:25
Ivan ButtinoniIvan Buttinoni 3,9821 gold badge21 silver badges39 bronze badges Background cron job sounds like a good idea for this. You'll need ssh access to the machine to run the script as a cron.
answered Jan 7, 2011 at 15:08
IanIan 22.8k22 gold badges56 silver badges96 bronze badges 2 If you
can access the server over ssh and can run your own scripts you can make a simple fifo server using php (although you will have to recompile php with The server can be written in anything really, you probably can easily do it in python. Or the simplest solution would be sending an HttpRequest and not reading the return data but the server might destroy the script before it finish processing. Example server :
Example client :
answered Jan 7, 2011 at 16:26
OneOfOneOneOfOne 90.6k19 gold badges175 silver badges178 bronze badges If you're on Windows, research But if we're on the same server "Linux" running cpanel then this is the right approach:
Don't use Lastly, on the
answered May 9, 2014 at 19:47
1 Assuming you are running on a *nix platform, use EDIT: There are quite a number of questions asking for "running php without cron" on SO already. Here's one: Schedule scripts without using CRON That said, the exec() answer above sounds very promising :)
answered Jan 7, 2011 at 15:08
Spiny NormanSpiny Norman 8,1721 gold badge32 silver badges54 bronze badges 1 for background worker i think you should try this technique it will help to call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous. form_action_page.php
testpage.php
PS:if you want to send url parameters as loop then follow this answer :https://stackoverflow.com/a/41225209/6295712
answered Dec 19, 2016 at 15:42
Hassan SaeedHassan Saeed 5,1671 gold badge30 silver badges34 bronze badges 0 In my case I have 3 params, one of them is string (mensaje):
In my Process.php I have this code:
answered Jun 23, 2015 at 18:58
This is works for me. tyr this
answered Aug 25, 2018 at 10:22
1 Use Amphp to execute jobs in parallel & asynchronously. Install the library
Code sample
Fo your use case, You can do something like below
answered Apr 21, 2020 at 8:14
SkyRarSkyRar 90917 silver badges33 bronze badges Not the answer you're looking for? Browse other questions tagged php scripting background or ask your own question.How to run PHP process in background?So, to run any background process from PHP, we can simply use either exec or shell_exec function to execute any terminal command and in that command, we can simply add & at the last so that, the process can run in the background.
How to run a PHP script in background in Linux?You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while background command is running.
How do I know if PHP script is running in background?If you started it in background use ps aux | grep time. php to get PID. Then just kill PID . If process started in foreground, use to interrupt it.
How do I run a PHP script continuously?Cron jobs are timed jobs that can for example execute a PHP script. You could set up a cron job to check which user should receive his/her sms every hour. Depending on your operating system you can set up these cron jobs. For linux distrubutions there are tons of guides on how to set this up.
|