Php – Running bash script with php under lower priority or limit CPU

bashcpu-usagePHP

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.

Best Answer

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