3

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?

gX.
  • 922
  • 1
  • 11
  • 18

4 Answers4

5

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."

JohnP
  • 49,507
  • 13
  • 108
  • 140
  • 1
    Well, just tried it and it waits for the other script before returning. – gX. Feb 24 '11 at 11:46
  • 3
    "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." - Mentioned in the Notes section – JohnP Feb 24 '11 at 11:50
  • Wonderful, thanks! When I first tried it, it went into an infinite loop for no reason at all. And so I used /usr/bin/php-cli thanks to this question http://stackoverflow.com/questions/3615713/exec-cause-an-infinite-loop-of-starting-requested-program – gX. Feb 26 '11 at 06:36
  • @cwallenpoole you're right, that comment should be part of the answer. I've updated it, thanks. – JohnP May 02 '11 at 15:17
1

Other solution :

  • Your script put a token somewhere (DB/filesystem) when a mail needs to be sent (token may contain data for mail generation)
  • A daemon (can be written in php) runs in the background looking for tokens and "eats" them to send mail

I use this kind of solution quite a lot ...

yent
  • 1,303
  • 1
  • 8
  • 10
  • Oh I tried your solution first, but I'm using a shared hosting and the daemon PEAR lib wanted me to recompile PHP with some options. But if I ever have my own server, this is the way I'm going to use :) Thanks – gX. Feb 26 '11 at 06:38
1

I am using jQuery and AJAX to do stuff like this for PHP scripts which take long time.

Juris Malinens
  • 1,251
  • 1
  • 9
  • 13
  • Well yes I understand, but in my case the script was supposed to run after a POST to another page and some heavy checking on the server end. So, not a very a safe way in my case. – gX. Feb 26 '11 at 06:40
0

What you want to use is something called beanstalkd

Details: How can I get a list of all jobs in a beanstalk tube?

It will allow you to queue up jobs (do 1, then 2, then 3. then 4) you can even distribute these tasks across servers (yes for php jobs!).

Community
  • 1
  • 1
Jakub
  • 20,418
  • 8
  • 65
  • 92