Super User is a question and answer site for computer enthusiasts and power users. 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

How can I limit the processor usage limit for the copying process when it is running? My friend and I are planning to get a PC to host servers and we need to know how to limit this usage to the copying process because last time we did a host machine, every time we were copying new files the servers were starting to lag.

share|improve this question

migrated from Aug 11 '12 at 19:48

This question came from our site for professional and enthusiast programmers.

It's probably that the filesystem you are using will play a larger factor in CPU usage than the cp process itself. And the fs work is going to be system usage, not user. – Jon Lin Aug 11 '12 at 19:33
Copying files from where to where? If you are copying files over the network it would be logical for the server to lag... – thkala Aug 11 '12 at 19:40

Use nice for CPU

The CPU scheduling priority is handled with nice(1), which says:

Run COMMAND with an adjusted niceness, which affects process scheduling. With no COMMAND, print the current niceness. Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).

Use ionice for Disk I/O

Using ionice -c3 will use idle priority for disk I/O. The ionice(1) manual page says:

A program running with idle io priority will only get disk time when no other program has asked for disk io for a defined grace period. The impact of idle io processes on normal system activity should be zero.

Combine the Two Options

For the least impact on your system, you can combine the two. For example:

# Run script with lowest possible CPU and disk priorities.
nice -n19 ionice -c3
share|improve this answer
There also is schedtool, which sets general scheduler policy (including I/O). – grawity Aug 11 '12 at 20:47
do you have to repeat the nice ionice combination if you use piped commands, e.g. nice -n19 ionice -c3 grep xyz | grep zzz ? Is grep zzz also niced or do you have to repeat the nice ionice in front of the grep zzz, means nice -n19 ionice -c3 grep xyz | nice -n19 ionice -c3 grep zzz ? – bzero Aug 15 '12 at 6:18
@bzero nice and ionice propagate to subshells, so you'd need to (io)nice the PPID or enclose your pipelines in (...) or {...;} if you want that to work. – CodeGnome Aug 15 '12 at 13:12
@CodeGnome : like nice -n19 ionice -c3 grep xyz | (grep zzz) ? – bzero Aug 16 '12 at 14:04
No, you can only do something like (renice -n 19 -p $BASHPID; ionice -c 3 -p $BASHPID; foo | bar) or nice -n 19 ionice -c 3 bash -c "foo | bar". (Note that { ...; } doesn't actually run a subshell.) – grawity Aug 24 '12 at 1:02

Copying has very little CPU impact, the resource it uses is IO bandwidth.

You should prefix io-intensive commands with ionice -c2 -n6 if you don't want them to impact other applications too much. You can also confine stuff using cgroups and the blkio controller, but that's more involved.

share|improve this answer

You must log in to answer this question.