Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is the situation:

I have a PHP/MySQL web application that does some PDF processing and thumbnail creation. This is done by using some 3rd party command line software on the server. Both kinds of processing consume a lot of resources, to the point of choking the server. I would like to limit the amount of resources these applications can use in order to enable the server to keep serving users without too much delay, because now when some heavy PDF is processed my users don't get any response.

Is it possible to constrain the amount of RAM and CPU an application can use (all processes combined)? Or is there another way to deal with these kinds of situations? How is this usually done?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Run it with nice -n 20 ionice -c 3

That will make it use the remaining CPU cycles and access to I/O not used by other processes.

For RAM, all you can do is kill the process when it uses more than the amount you want it to use (using ulimit).

share|improve this answer
    
And is it possible to do this when starting the process from php? Or to config this somewhere so the applications I'm using always execute using nice? Killing RAM sounds like it can cause errors in the process? –  Jeroen Moons Aug 29 '12 at 11:12
    
@JeroenMoons It's not about "killing RAM", it's about killing the process. –  Chris Down Aug 29 '12 at 13:18
    
Ahhh ok, big difference :) –  Jeroen Moons Aug 29 '12 at 13:20

You can run it from php, but ionice needs to be run as root, so either use sudo to run the ionice or run that php script(s) under a server process that was run with ionice.

If you use fastcgi you could have two different engines running and give the high resources work to the lower priority one.

If you use Apache without fastcgi, you'll have to use sudo to run the command, make sure to make the sudo permission as specific as possible, otherwise you're risking a security breach.

share|improve this answer

Your Answer

 
discard

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.