Jump to content

Limit CPU usage in PHP


doc1355

Recommended Posts

Hi:

I have a php script that gathers some information from different tables in one database then add them in a specific way to another table.

The problem with my script is that running this script on my shared hosting exceeds the CPU limit of 60%. I received a message from my hosting company that my process is using move than 75% of processor. So, they have an automated system that kills my script.

Unfortunately I cannot change the max_execution_time on the server, because it is shared.

Now, what I'm looking for is that if there is a script that I can add to my php script that limits the processor usage by certain percent (for example 40%) then it sleeps every 25 seconds and resume again after 10 seconds. I know about the sleep command but I don't know how to use it in my script for this purpose.

Here is the script that I'm running:

 

<?php
set_time_limit(0);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);

// clean overall cache
Mage::app()->cleanCache();
echo "Cache Cleaned!<br/>\n";

Mage::getSingleton('catalogindex/indexer')->plainReindex();
echo 'Layered Navigation Indices was refreshed succesfuly<br>';
?>

 

Thank you.

Link to comment
Share on other sites

Now, what I'm looking for is that if there is a script that I can add to my php script that limits the processor usage by certain percent (for example 40%) then it sleeps every 25 seconds and resume again after 10 seconds. I know about the sleep command but I don't know how to use it in my script for this purpose.

 

There is no realistic way of doing this through php and apache. sleep itself uses cpu while your script would need to sit within a loop.

 

I have a php script that gathers some information from different tables in one database then add them in a specific way to another table.

 

Seems you may be better off executing this script via the cli and cron.

Link to comment
Share on other sites

I used too much CPU aswell

That is what my hosting company told me

So, they suddenly shut my site down

I was kinda angry

They said because you are on a shared Server

So, i then got my Dedicated server

 

Perhaps this is what you should do if your CPU is always high

 

Or check your scripts, perhaps something is causing Apache to get confused

Link to comment
Share on other sites

Seems you may be better off executing this script via the cli and cron.

Even running via cron is killing my script.

... So, i then got my Dedicated server

Perhaps this is what you should do if your CPU is always high

Will, I was thinking about upgrading to VPS, but this exec time is the only limit that I have with my shared server and I just want to run this file once every few days. I don't really want to spend twice as I'm spending on my hosting fee only for one small problem. That's why I'm looking for a solution.

Link to comment
Share on other sites

Ohh, i understand what you mean, you dont want to pay extra for running a script for few days

Actually, you are correct

 

If i was in your case, i will move to a new hosting company (With the same Shared hosting plan)

 

And hope that they will not mention anything about the CPU usage

 

Hopefully their servers are much faster and wont cause no problems

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.