Most people know that you can run commands "in the background" on UNIX by prefixing them with "nice":

nice run_long_script.sh

What that does of course is reduce the priority of the process so that the CPU scheduler only runs it when nothing else (of a higher priority) wants to run. What it doesn't do however is run the process without any impact whatsoever on your other tasks. The reason is that the background process can still ruin the performance of your computer by monopolizing the hard disk.

It turns out that Linux 2.6.13 or later (with the CFQ scheduler) support a tool called ionice which is a "nice" command for I/O operations (like disk accesses). It works like this:

ionice -c3 command

The only problem is that you can only reduce the IO priority of a process as root. The reason being that if you're not careful you could have a high priority (CPU priority that is) IO-blocked by a lower-priority process.

But if you run your compilation like this:

sudo ionice -c3 make

it will run the whole thing as root, which is probably not what you want. So you could do something like:

sudo ionice -c3 sudo -u myusername make

to run the compilation as yourself. Though one problem persists: sudo strips out your environment. So things like distcc and ccache may not work anymore.

Here's how I solved these problems in my background compilation script for custom kernel Debian packages:

#!/bin/bash
nice fakeroot make-kpkg --revision=custom.1.0 kernel_image &
BACKGROUND_BUILD=$!
sudo ionice -c3 -p $BACKGROUND_BUILD
wait $BACKGROUND_BUILD || exit 1  

I just use ionice's -n option instead of -c. It seems to work OK.

eg. ionice -n7 btdownloadcurses ...

I do wish that I didn't have to consider IO scheduling priority as distinct from CPU scheduling priority. Maybe I will need to write a wrapper after all.

Comment by Ted