continuous PHP script

I have a PHP script running in a non-stop loop, much of the time it does nothing but every 2-10 minutes it does some actions. So I am running it as a daemon I guess. CPU usage on my linode 512 is a continuous 95%, I asked support if this is acceptable and they said yes since it is percentage of all 4 cores and the maximum is 400% so in effect it is really less than 25%, however I am wondering if there is more effective way to achieve my needs with lower CPU usage. I would have thought the average would be much lower with spikes but the level is flat and constant, it would seem just running a PHP script uses a lot of CPU even if that script is doing nothing other than a code loop.

The reason am doing it this way is I need it to react at certain times of day within accuracy of a few seconds, something I can't achieve with cron jobs, and also it is not the same schedule every day, each day there is a different set of times I need the tasks to run.

Regards,

Geoff

9 Replies

PHP is not really designed for that purpose. A lot of overhead is running while it is.

If it is email or web generated, it should be easy to have it trigger an event which causes it to be processed. The trigger could be a shell, Perl or other script which can call PHP if necessary to perform the action.

Not really enough info provided.

What is it doing during the loop if it's not time for it to run? If it's not calling sleep() somewhere in there, yes, it will burn up a lot of CPU. Try sticking sleep(1) in there, or whatever PHP's notation for it is.

Many thanks!, adding a sleep(1) into every loop cycle has made a MASSIVE difference, now the CPU usage is almost nothing while it's looping doing nothing, spiking at 15-20% only when the other time triggered activities occur.

Indeed, CPUs are really good at doing nothing. :-) Glad to know that helped!

How is it reacting? If it's listening for incoming packets or connections, you should be using socket_select(), which will block waiting on activity on any of the sockets, and immediately wake up your app when there is activity. It also has a timeout, so you could set it to time out once a second to do other stuff periodically, for example.

Does anybody remember that name of the PHP framework for writing daemons? The one whose philosophy was something like "PHP usually isn't the best thing to do this in, but if you have to, this will make it as easy as possible"?

@Guspaz:

How is it reacting? If it's listening for incoming packets or connections, you should be using socket_select(), which will block waiting on activity on any of the sockets, and immediately wake up your app when there is activity. It also has a timeout, so you could set it to time out once a second to do other stuff periodically, for example.

Does anybody remember that name of the PHP framework for writing daemons? The one whose philosophy was something like "PHP usually isn't the best thing to do this in, but if you have to, this will make it as easy as possible"?

https://github.com/shaneharter/PHP-Daemon

Warning, have lube on standby. Doing this kind of thing in PHP is not gentle.

I wrote a long-lived PHP daemon back in the day (years ago, an IRC bot that managed some channels, supported custom plugins, etc) and it seemed to work pretty well. What makes PHP or Java well or poorly suited for writing a daemon? Is it just a matter of an inefficient garbage collector or something?

PS: Writing the IRC bot was a fun exercise. Funny how a lot of the programming-for-fun that I do falls into the "useless or unnecessary but fun" category. Loading plugins dynamically by including them when executed, for example, is basically a memory leak. Running the plugins in a separate instance of the PHP interpreter with a standardized manner of passing information back and forth, on the other hand, worked better.

@Guspaz:

I wrote a long-lived PHP daemon back in the day (years ago, an IRC bot that managed some channels, supported custom plugins, etc) and it seemed to work pretty well. What makes PHP or Java well or poorly suited for writing a daemon? Is it just a matter of an inefficient garbage collector or something?

PS: Writing the IRC bot was a fun exercise. Funny how a lot of the programming-for-fun that I do falls into the "useless or unnecessary but fun" category. Loading plugins dynamically by including them when executed, for example, is basically a memory leak. Running the plugins in a separate instance of the PHP interpreter with a standardized manner of passing information back and forth, on the other hand, worked better.

Mmm, GC is actually nicely done in 5.3+, that's not the real issue - threading/forking is when you start getting into very very painful things. Single-threaded daemons that don't mind blocking is fine, but non-blocking execution is absolutely frightening.

(I wrote my own IRC bot in php as well, turned out quite well with uptimes of up to two months without leakage or breakdowns of any sort, and was a great learning experience - am currently rewriting her in node.js to learn node itself, and take advantage of node's async behavior)

Threading/forking in PHP is just barebones POSIX stuff, though… It should be the same experience as doing it in C, no?

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct