Using nice to control CPU usage

  • July 9, 2007
  • Avatar for peter
    Peter
    Upfold

Managing tasks that use up a lot of CPU time can be quite a task and if you're working in a shared environment where you don't want to be antisocial and use all the available resources, it can be necessary to manage the tasks you are running to ensure you are being nice.

And nice is exactly the command you need to use for this purpose. What it allows you to do is force a command to run with a different priority level. The levels range from -20 (top priority) down to 19 (lowest priority).

It's pretty simple to use, simply prefix the desired commands you want to run with the following:

$ nice -n prioritylevel commandtorun

For example, I could run a backup with tar with lowest priority:

$ nice -n 19 tar -cjvf /backup.tar.bz2 /folder/to/backup

nice is available on literally all Linux systems and almost all other Unix-like operating systems too. Some versions may differ very slightly, but the basic functionality will remain the same.

Avatar for peter Peter Upfold

Home » Articles »