0

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.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
scotts
  • 237
  • 2
  • 9

1 Answers1

1

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

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

Satanicpuppy
  • 5,946
  • 1
  • 17
  • 18
  • 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