3

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.

3 Answers 3

5

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

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

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.
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .