Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call another PHP script and return control to user before the other script completes

Tags:

php

email

cron

I'm actually trying to apply this to a script that sends email. The sending email part takes a few seconds which is too long. What I want is for the first script to do its stuff and trigger another script (which sends the Email), but I want the first script to return control to the user without waiting for the second script to send the email.

Options that I have considered:
Cron Job: For this I'll have to have a cron job run something like every 2 mins. Not feasible!
PHP script activating a cron job which deactivates itself when done: Well ok, but how do I do this? Can PHP do this?

like image 281
gX. Avatar asked May 22 '26 18:05

gX.


1 Answers

You can also call the shell and manually call the PHP file. No cron required and no waiting.

http://www.php.net/manual/en/function.exec.php

From the Notes Section

"If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends."

like image 178
JohnP Avatar answered May 25 '26 08:05

JohnP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!