# limit cpu ocupation for script

How can I limit a cpu usage to my backup script. It does a mysql db dump and backup of my www directory then bzip2 everything, but it takes a bit and during this time a cpu has 100% load. Can I somehow limit a cpu to i.e. 25@ at most, it will take a more time to make a backup but won't halt other background processes.

-

You don't want to limit its CPU usage by percentt - after all, if the CPU has nothing else to do, it might as well dedicate 100% to your script.

You want to make sure other programs have priority over your backup script. To do this, just invoke the script using nice:

nice mybackupscript


This will run the script with low priority, that way it will only get the CPU if other processes are idle.

-

cpulimit does exactly what you need.

-

Often with backup scripts the bottleneck is not CPU but disk I/O. If this is the case, nice will make little difference to the load on your system. iostat, which is included with SYSSTAT, can help you investigate further. Also, this post details other I/O utilities.

ionice might help some. However, ionice will only work with the CFQ scheduler. CFQ became the default with > Linux 2.6.18. RHEL4 provides the 2.6.9 kernel, however it is still default on RHEL4 and newer.

-
+1 for mentioning I/O –  sleske Jul 11 '10 at 22:59

Run script with the lowest priority:

nice -39 /scripts/script_name.sh

-
-1 negative values increase priority (not to mention that the valid range is -20 to 19 inclusive). –  sleske Jul 11 '10 at 22:58
nice -39 runs with least favorable priority (19) –  vitalie Jul 14 '10 at 7:58
Lowest negative value for nice is -20 so it gets overflowed and becomes 19 :) , however I do agree that it's more clearly to use "nice 19" than "nice -39", but they are doing the same thing. –  vitalie Jul 14 '10 at 8:19
BTW, the thing about "overflowing" is also nonsense. "nice -n" (n a number) will set niceness to "n". n>19 is the same as 19, so nice -19, nice -20, nice -21... etc. all do the same. –  sleske Dec 9 '10 at 23:38