Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently I've a programme taking up too much of CPU.

How could I limit the amount of CPU it occupies?

OS is Linux, Fedora.

I can't modify the source code of that programme.

What I need is a Bash command.

share|improve this question
You should probably provide more detail. Which language and code libraries, etc. – Frank Jun 12 '09 at 20:25
Are you trying to use the OS or an outside application to limit the usage or programming the application to limit itself to 50% usage? – Chris Porter Jun 12 '09 at 20:27
Run two of them... – Ryan Oberoi Jun 12 '09 at 20:34
50% doesn't mean alot when speed, core capacity and quantity are abstract. What you limit is TIME. – Aiden Bell Jun 12 '09 at 20:34
Recommending a close under "Not programming related" considering shore commented "No programming". – Aiden Bell Jun 12 '09 at 20:37

You can try limiting your program by lowering the priority with nice. No programming involved there.

share|improve this answer
Limiting usage by priority seems to be the best approach to me. If your application is of low importance but is over using resources and causing other applications to suffer, prioritization solves that. If your application is not affecting other applications there is no reason to slow it down by usage. – Chris Porter Jun 12 '09 at 20:30
"there is no reason to slow it down by usage" - unless your fan gets noisy when CPU usage is above X%, in which case you probably don't want your dnet client running at more than that. That's pretty specific though to apps actually designed to consume massive amounts of CPU indefinitely: normally you want to Get Stuff Done. – Jun 12 '09 at 20:34
Can someone make a demo usage? – Shore Jun 12 '09 at 20:36
Sure... "nice -n <priority> <program>" where priority goes from "-20" for very high, "0" for normal and "20" for lowest possible. If the hypothetical program was called "server", and low (but not lowest) priority was desired, this would work: nice 10 server – Alexandre Carmel-Veilleux Jun 12 '09 at 20:59

Running at "50%" CPU isn't that meaningful. You want the program to use every resource possible when it's available. If the CPU isn't doing anything else, that program might as well make full use of it. If you wanted the program to really do nothing at all, you'd have to modify the source code and put in pauses/sleeps where possible.

What you want is to have everything else have higher priority. See the manpage for the nice command, run it at nice 19

Another and possibly more effective way of limiting resources is to install the schedutils package, and run the program using the SCHED_BATCH process scheduler.

share|improve this answer

You're looking for something simple and fast? Try the cpulimit program. Just run:

cpulimit name-of-program

and voila, it's limited.

share|improve this answer

Setrlimit and co ...

#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>

int main ()
 struct rlimit rl;

 /* Obtain the current limits. */
 getrlimit (RLIMIT_CPU, &rl);
 /* Set a CPU limit of 1 second. */
 rl.rlim_cur = 1;
 setrlimit (RLIMIT_CPU, &rl);
 /* Do busy work. */
 while (1);

 return 0;

From here

share|improve this answer
No programming... – Shore Jun 12 '09 at 20:33
Thanks for that Shore. It was appended to the question, which I did read in its first form before your edit. – Aiden Bell Jun 12 '09 at 20:36
could you tell me what's the relationship tween serverfault and here?why must transfer this question there? – Shore Jun 12 '09 at 20:40
Same people run both. One is for programming questions, and the other is for non-programming questions (like this one became after your edit) – Aiden Bell Jun 12 '09 at 20:41

If you don't want to modify the program another option to consider is virtualisation.

share|improve this answer

If you want to limit a process's cpu, based on the concept of percentage, consider cpulimit.

share|improve this answer

You can manually do time-slicing within the application by using a high-performance timer and measuring how much time each iteration of a top level loop is taking, then put in appropriate sleeps (or nanosleep's) in that loop. This wont correlate directly to a percentage of CPU, especially across machines, but it will limit the cpu resources that the program takes.

share|improve this answer

Take a look at Control Groups.

LWN has an article about them.

Redhat, Fedora, CentOS have an RPM package named libcgroup that has several handy command-line tools, a system daemon and some config files to manage control groups.

This is based on libcg hosted on SourceForge.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.