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.

Wesley
  • 32,690
  • 9
  • 82
  • 117
Dave
  • 157
  • 6

3 Answers3

5

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

KM.
  • 1,786
  • 2
  • 18
  • 31
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.
msanford
  • 1,477
  • 15
  • 28
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.

David Schwartz
  • 31,449
  • 2
  • 55
  • 84