Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a bash script which runs a set of php scripts. When it runs it takes an hour and pegs the CPU at 95-99%. This causes our lamp stack (mainly the apache process) problems and our website on the same server starts spitting out timeout or 500 errors.

How can I either:

  • Run the bash script and all associated tasks (php scripts which also call the PostgreSQL db) at a low priority so apache, php and db tasks for the web server are always prioritized, OR
  • Limit the CPU usage for the script and associated tasks to, e.g. 25% CPU

I'm not sure which is the better solution.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

In addition to nice, looking at limiting CPU time, using ulimit -t (assuming you are on a *nix platform)

share|improve this answer
    
Didn't know about that one! –  msanford May 23 '12 at 23:48
    
Thanks. So does nice also prioritize other processes kicked off by the bash script? –  DaveR9 May 24 '12 at 1:30
    
Normally, all processes inherit nice value of their parent process when they are spawned. –  msanford May 24 '12 at 3:52

Run your commands through nice.

nice runs utility at an altered scheduling priority. If an increment is given, it is used; otherwise an increment of 10 is assumed. The super- user can run utilities with priorities higher than normal by using a neg- ative increment. The priority can be adjusted over a range of -20 (the highest) to 20 (the lowest).

 Available options:

 -n increment
         A positive or negative decimal integer used to modify the system
         scheduling priority of utility.
share|improve this answer

As others have suggested, nice is what you want. Limiting to 25% of the CPU is boneheaded. This will needlessly extend the amount of time that system performance is reduced.

Say you have four cars and need to do a few errands. Which makes more sense, using one car to do the errands or using all unused car to do the errands? The former leaves 4 cars for important tasks. The latter leaves only 3. And in most realistic cases, the former will also get the low-priority job done sooner.

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.