How can i monitor php script cpu usage?

Discussion in 'PHP' started by sanyi007, Oct 21, 2009.

  1. #1
    Hi all!

    I'm on a shared hosting and i would like to make my wordpress site as lightweight as possible.

    How can i monitor the cpu usage of the php scripts?

    I hope there is a possibility for shared host users, because i hate to get "account suspended for high cpu usage" messages!
     
    sanyi007, Oct 21, 2009 IP
  2. superdav42

    superdav42 Active Member

    Messages:
    125
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Depending on how your account is setup you can use ssh into the server then run top to see what is using the cpu, your php script may or may not show up, depends on which user it is ran as because top will only show your own processes. You can also use the server info button in cPanel to see the load of the whole server which might be helpful.

    There are a few techniques you can do to reduce the load your php script put on the server

    If it is being ran from a cron job you could use the nice command to set it to a lower priority.

    You could insert sleep() at certain lines in your script to make the script wait a bit before continuing.

    If it's database queries that are causing the load you could try using the keyword LOW_PRIORITY in your insert and update queries.

    I had an import script that was getting me suspended and I was able to fix it using these techniques. Mostly I just made it sleep a lot.
     
    superdav42, Oct 21, 2009 IP
  3. phprightnow

    phprightnow Peon

    Messages:
    296
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Really depends on how you're running it. If you're running via command line in the background the top command can show you your top running processes, along with their CPU and memory usage stats.
     
    phprightnow, Oct 21, 2009 IP