Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a sandbox on my server where I allow users to run their own PHP scripts. If a PHP script has an infinite loop, it only stops when Apache times the process out (30 seconds) which is obviously less than ideal. I know some web hosts automatically kill a PHP script consuming too much CPU for too long. How do I implement something like this at the server level in Linux, but without killing PHP processes that just take a long time (with sleep statements, or waiting for network latency, for example)?

And as a bonus question, how do I do the same with memory usage? I know PHP has built in memory limits, but included in this sandbox is a PHP extension that doesn't implement these limits until control returns back to PHP, at which time if the process went over the limit it's killed, but by then it's too late. It could have swapped out the entire swap disk in the meantime, bringing the server to a crawl.

share|improve this question
up vote 1 down vote accepted

You can lock it down in Apache with RLimitCPU and RLimitMem.

Or you can renice the whole process at intervals from the OS itself.

share|improve this answer
Sounds like RLimitCPU/RLimitMem won't work with PHP running under mod_php (the conventional way) because it only applies to processes spanwed from Apache, not Apache processes themselves. – scotts Oct 16 '09 at 16:49
Then use nice and renice on Apache itself to limit the cycles it's allowed to steal. – Satanicpuppy Oct 16 '09 at 18:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.