0

I'm looking for some ideas to do the following. I need a PHP script to perform certain action for quite a long time. This is an extension for a CMS and this can't be anything else but PHP. It also can't be a command line script because it should be used by common people that will have only the standard means of the CMS. One of the options is having a cron job (most simple hostings have it) that will trigger the script often so that instead of working for a long time it could perform the action step by step preserving its state from one launch to the next one. This is not perfect but I can't see of any other solutions. If the script will be redirecting to itself server will interrupt it. What other options can suit?

Thanks everyone in advance!

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
AndreyM
  • 119
  • 2
  • 11
  • 1
    Right, we don't want your thanks in advance, we want your thanks after the fact ;) – andrewtweber Feb 04 '12 at 17:30
  • Functionality in PHP doesn't just get removed over night. It's more than likely that your CMS (depending on complexity) will break because of PHP upgrades before your script does it. – kba Feb 04 '12 at 17:33

2 Answers2

2

What you're talking about is a daemon or long running program that waits for calls by client programs, performs and action, provides a response then keeps on waiting for more calls.

You might be familiar w/ these in the form of Apache & MySQL ;) Anyway PHP is generally OK in this regard, it does have the ability to function over raw sockets as well as fork sub-processes to handle multiple requests simultaneously.

Having said that PHP daemons are a tool where YMMV. Some folks will say they work great, other folks like me will say they have issues w/ interprocess communication and leaking memory even amidst plethora unset() calls.

Anyway you likely won't be able to deploy a daemon of any type on a shared hosting environment. You'll need to get a better server package or stick with a Cron based solution.

Here's a link about writing a PHP daemon.

Also, one more note. Daemons do crash from time to time and therefore you may still need to store state about whats going on, just in case someone trips over the power cord to your shared server :)

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
  • Thanks. This is a good option but since it's going to be a part of widely distributed software most people won't be able to launch the script as a php daemon – AndreyM Feb 04 '12 at 18:48
  • Ok that's fine, but thats essentially what a program that 'runs forever' is. If you're thinking about a wide distribution then you may even cross cron off the list if Windows is a desired platform. There are some techniques to emulate long running programs inside of Apache [look here](http://moxune.com/blog/2011/04/non-blocking-web-service-processing-in-php/) but they're best left to short programs. Anyone who has root access on *nix system can run/install a daemon. If shared hosting systems are the target of the software cron is probly the way to go. – quickshiftin Feb 04 '12 at 18:59
  • Yes, I know what a daemon is, it would be a really great option unless this was for people who only can download an extension and install it using standard means of the CMS (choose an extension file and press "Install"). – AndreyM Feb 04 '12 at 19:28
1

I would also suggest that you think about making it a daemon but if not then you can simply use

 set_time_limit(0);
 ignore_user_abort(true); 

at the top to tell it not to time out and not to get interrupted by anything. Then call it from the cron to start it every day or whatever. I have this on many long processing daily tasks and it works great for me. However, it won't be able to easily talk to the outside world (other scripts can't query it or anything -- if that is what you want look into php services) so once you get it running make sure it will stop and have it print its progress to a logfile.

hackartist
  • 5,172
  • 4
  • 33
  • 48
  • I had this in mind too but I wonder if hosting providers monitor their servers to discover and interrupt such scripts. Also I wonder if there is a way for hosting providers to disable ability to change the options you mentioned. – AndreyM Feb 04 '12 at 19:15