Limit CPU usage of intensive tasks
In this article we'll discuss how you can limit the CPU usage of an intensive task and spread it out over a longer period of time. This can help ensure that you aren't causing problems of sluggishness on your own website, or affecting other users on the same server while running an intensive task.
Limiting your CPU usage is a more advanced task that would require a VPS or dedicated server hosting plan. On a VPS you can limit CPU usage to help ensure that you're remaining within the limits of your VPS package, so that you don't affect your VPS neighbors sharing the same physical hardware with you and possibly risk a temporary suspension. On a dedicated server you can limit the CPU usage of these intensive tasks to help ensure you don't slow things down overall on your own server.
For instance lets say that you run a nightly backup of your site's files, this type of task typically isn't too time sensitive, so letting the intensive archiving of the backup process run over the course of an hour or so should be fine. If the server is busy at the time it would need most of its CPU time to handle website/email/database requests, and if you throw an intensive task in the mix it could make everything slow down.
cPanel comes with a script called cpuwatch which allows you to watch the current load of your server while a process is running. If the load on the server exceeds the limit that you've set for the load, it will go ahead and pause the process and leave it paused until the server has become stable again. This is a great way to start something intensive and set it and forget it, without having to worry about it bogging down your server.
In the example below I'll be backing up a /public_html/ directory to an archived file and limiting the CPU usage so it doesn't slow down the server at all.
- Login to your server via SSH
- If you're on a VPS you'll always want to set the CPU limit to 1 or lower (.9 .8 .7 etc...), or on a dedicated server you can run the following command to see how many processors you have which will affect how high your load average can get before things start running slow:
grep pro /proc/cpuinfo -c
This should simply give back the number of cores: 4
- Now you can enter in the path to the cpuwatch script, followed by the limit on how high the load can get before the task is paused, followed by your task to run:
/usr/local/cpanel/bin/cpuwatch 4 tar cvzf 11-30-2012.tar.gz ./public_html/
In this case we are setting the limit to pause at to a 4, using the tar cvzf command to archive files into a file called 11-30-2012.tar.gz, and we're doing this to all files in our /public_html directory.
Because we used the verbose option on our tar command, we'll see the active file that is being archived as the task runs. While this is happening if our server's load reaches a 4 or higher you'll start seeing that the process gets paused until it falls back below that threshold.
./public_html/.htaccess ./public_html/index.php ./public_html/images/logo.gif Waiting for load to go down to continue Waiting for load to go down to continue Waiting for load to go down to continue ./public_html/images/magnify.gif ./public_html/images/nested.gif
You should now understand how to use the powerful cpuwatch script to help keep your intensive tasks from overloading your server. You can also use cpuwatch in your cronjobs so you don't have to be sitting there at the console to take advantage of it.