Link to home
Start Free TrialLog in
Avatar of djs120
djs120

asked on

How to fire off PHP script/page in background?

I have a PHP script that rolls through a large table of email addresses and sends out a newsletter based on custom content drawn from the database.  On average I calculate this script to take 2 minutes or so to run.

Is there a way to fire off the process in the background so that the page does not have to wait to be loaded in the browser?  I am afraid that those who are running the mailing script will close the window or hit stop and screw things up.  Can I somehow run it in the background or are there other ideas?  Thanks!

FYI, I am on a shared hosted webserver, so I don't have root access to anything.
Avatar of Kooroo
Kooroo
Flag of United States of America image

a couple of options. but I have to ask, this will be a user initiated request to fire off these emails?

One way to do it is via a trigger somewhere.
-Essentially, when a user visits your page and submits a request to fire off the newsletter, all your script does is create a 0-byte file in your home directory.
-You have a cron job running under your username(most shared hosting have control panels to create cron/scheduled jobs) that runs every minute. All this job does is this:
if trigger file exists, call the php script via a curl or just a shell execute since it won't return anything, then erase the trigger file.

so now what you've done is taken away the long running process from your end user and turned it into a trivially short one (touch a file). and the cron job should also be trivially small until it senses that you actually need the job run so it shouldn't be too big a deal to run it fairly frequently.
ps. - also, this will keep multiple users from sending out your mailing list a bagillion times simultaneously and pissing off your host.
pps - don't forget to double check with your host that you are able to send out all these emails off their smtp.
Avatar of djs120
djs120

ASKER

Unfortunately my host is really crappy and I can't set up cron jobs from the control panel.  I doubt the host will set one up for me.  Is there something I can do using the PHP exec() command or system() command (or something close to that?).
what host is this? do they offer shell access? you could do it via command line if they do.

another solution is to look at the reference for the php function set_time_limit(). This isn't an absolute solution though because you'll still be bound by global settings...but it should prevent users from messing your script up with a stop.

It is my opinion that the cron job is best because it eliminates user misfunction fairly cleanly.
all you need to do is ignore user abort.  depending on they type of server depends on the setting.  true for *nix, false for some versions of windows.

just add to the top of the script:
ignore_user_abort(true); // for *nix
ASKER CERTIFIED SOLUTION
Avatar of wasco
wasco

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marcus Bointon
If you can get decent access, this is the 'proper' way to keep a PHP script running all the time in the background:

https://www.experts-exchange.com/questions/20977409/Execute-a-PHP-script-every-2-mins.html

I use this mechanism for sending out mailing lists.
Avatar of djs120

ASKER

I like your idea, wasco, with showing the user progress on the task.  I will try implementing this and will return to post the solution here...
Avatar of djs120

ASKER

Wasco, I used your idea and did the % bar feature by using a META REFRESH to change the location and display how many records have been completed so far:

<META HTTP-EQUIV="Refresh" CONTENT="1; URL=<?=$_SERVER['PHP_SELF']?>">

Thanks!