Restrict php cpu load

Hi, Im running Mandrake 9.2 and php 4.

I am also running a few game servers and I generate player stats for these servers using php. Whenever php runs on the server, it uses 99% of the cpu and causes the gameservers to lag and people often leave the server because it becomes unplayable for about a minute whilst php is running.

Is there anyway to restrict the amount of cpu usage php gets so it wont cause lag on the game servers when its running?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi choccarlm,
Did you try "nice"? Or renice?



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
choccarlmAuthor Commented:
No I havent tried that, how does it work and how would I run it?

You can either do
nice -[numeric value 1-20] command ...

Example: nice -20 apache

...or you can re-nice existing processes:

renice 20 pid ...
Such as:

ps -ef | grep apache | awk '{print $2}' | xargs renice 20
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

The "nice" value affects CPU scheduling. You can see that with the "top" tool:
top -I

Processes with a higher nice value don't get an equal CPU share on a fully loaded machine. You could also use negative nice values for your game server when using root, but I wouldn't recommend that. Example:

renice -5 <server_pid>
choccarlmAuthor Commented:
so nice -n 19 command would mean that the command being called would get less priority than other running commands?

Could I also start all 3 of my gameservers using the nice command? I.E

nice -n -20 /bin/gameserver1
nice -n -20 /bin/gameserver2
nice -n -20 /bin/gameserver3

and the stats using

nice -n 20 /bin/stats1
nice -n 20 /bin/stats2
nice -n 20 /bin/stats3
Sure, you can do that. It just depends which process should be a low-priority background process.
choccarlmAuthor Commented:
so the lower the value (-20) the higher the priority?
"nice -20" is equivalent to "nice -n 20" - it's a positive number. a numeric -20 would be --20. And yes, a negative nice value means a higher priority (a "less nice" process). Only root can lower the nice value.
choccarlmAuthor Commented:
ok so nice -n -20 would have a higher priority than nice -n 20 right?
Yes, a LOT higher. I know of no system process which uses such a high priority. Be careful.
- means higher priority
+ means lower pririty

Here is a table:

-19 = highest
0  - normal priority
19 - lowest priority!

Kernel won't allow a -20 .. -19 is the max you get i think.

choccarlmAuthor Commented:
thanks a lot, much appreciated
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.