title: High CPU usage with php command line scripts, is php-fpm the solution?
body: <p><strong>Update (2013 Oct, 1st):</strong></p>
  
  <p>Today I finally found out what was the issue. After debugging for hours with xdebug and trying to execute multiple scripts I noticed that the body of the script is not the problem. When executing tests worker with a sleep of 10-20 seconds I've noted that the CPU was idling most of the time and so deducted that was consuming the most of the CPU is to bootstrap Symfony.</p>
  
  <p>My scripts were very quickly executed and killed to pawn to new script, etc. I've fixed it adding a <code>do{}while()</code> that is exiting after a random amount of seconds (to avoid all the worker to restart at the same time). </p>
  
  <p>I've reduced the load from an average of 35-45% to an average of 0.5-1.5% That's a <strong>HUGE improvement</strong>. To Resume Symfony is bootstrapped once and after the script is just waiting until a random timeout to kill itself and launch a new instance of itself. This is to avoid script to hang or the database connection to timeout, etc.</p>
  
  <p>If you have a better solution do not hesitate to share. I'm so happy <strong>to go from 100% CPU usage (x4 servers because of the auto-scaling) to less than 1% (and only one server)</strong> for the same amount of work, it's even faster now.</p>
  
  <hr>
  
  <p><strong>Update (2013 Sep, 24th):</strong></p>
  
  <p>Just noticed that the console component of Symfony is using <code>dev</code> environment by default.
  I've specified <code>prod</code> in the command line: <code>./app/console --env=prod my:command:action</code> and I divide by 5 the execution time which is pretty good. </p>
  
  <p><del>Also I have the feeling that <code>curl_exec</code> is eating a lot of CPU but I'm not sure. </del></p>
  
  <p>I'm trying to debug the CPU usage using xdebug, reading the generated cachegrind, but there is no reference of CPU cycle used per function, class, ... Only the time spent and memory used.</p>
  
  <p>If you want to use xdebug in a PHP command line just use <code>#!/usr/bin/env php -d xdebug.profiler_enable=On</code> at the top of the script</p>
  
  <p>If anyone has a tip to debug this with xdebug I'll be happy to hear it ;)</p>
  
  <hr>
  
  <p>I'm asking this question without real hope. </p>
  
  <p>I have a server that I use to run workers to process some background tasks. This server is an EC2 server (m1.small) inside an auto-scaling group with high CPU alert setup.</p>
  
  <p>I have something like 20 workers (php script instance) waiting for jobs to be processed. To run the script I'm using the console component of <code>Symfony 2.3 framework</code>.</p>
  
  <p>There is not much happening in the job, fetching data from URL, looping over the results and insert it row by row (~1000 rows per job) in MySQL (RDS server).</p>
  
  <p>The thing is that with 1 or 2 workers running, the CPU is at 100% (I don't think it's like at 100% all the time but it's spiking every second or so) which cause the auto-scaling group to launch new instances.</p>
  
  <p>I'd like to reduce the CPU usage which is not justified at all. I was looking at php-fpm (fastCGI) but it looks like it's for web servers only. PHP client wouldn't use it? right?</p>
  
  <p>Any help would be appreciated, 
  Cheers </p>