26

When I run sudo apt-get upgrade with a lot of packages on my low-end laptop, I get a perceptible system slowdown, sometimes with up to 3 second screen freezes.

Is there any way to mark this job as very low priority, i.e. use very little CPU, MEM and HDD? I don't really care if it would make apt-get upgrade two hours longer to finish, I just want to keep working without the disruptions during the upgrade.

26

The command nice can be used to manipulate process CPU scheduling priorities. The command assigns a "niceness" value from -20 (most important) to +19 (least important) to the process. Root can assign any value, other users only positive ones (minor importance). The default value is 0.

nice -n <niceness> <command>

To set the priority of an apt-get command to the least value, you can use the command

sudo nice -n 19 apt-get upgrade

To set the priority of an already running process, the command renice can be used:

renice -n <niceness> -p <pid>

Edit: Thanks to @David for mentioning the ionice command, which lets you manipulate disk I/O priority. It can put a process into three different classes:

  • Idle only gives the process disk time, if no other process claims it at the moment.
  • Best-effort (default class). This allows you to assign priorities from 0 to 7, where 0 is most important and 7 least. You may try assigning -n 7 as the priority level.
  • Realtime processes are handled before everything else, suspending disk I/O for other processes, as soon as they require it for themselves. Use with care!

IOnice combines the syntax of nice and renice:

ionice [-c class] [-n level] command             #To start a new process
ionice [-c class] [-n level] -p pid              #To change a running process

Both commands can be combined, e.g.

sudo ionice -n 7 nice -n 19 apt-get upgrade      #Omitting the -c switch will assign Best-effort
sudo nice -n 19 ionice -n 7 apt-get upgrade
nice -n 19 ionice -n 7 sudo apt-get upgrade
...
  • 6
    I'm surprised this got so many upvotes. apt-get upgrade is IO bound! – Joshua Jan 1 '15 at 17:53
  • 3
    how about ionice? – David LeBauer Jan 1 '15 at 20:07
  • 1
    So, to clarify, the sudo in your command is not for nice, but for apt-get itself? It seems that nice -n 19 sudo apt-get upgrade also works for me. Is there any important difference? – wchargin Jan 2 '15 at 1:18
  • 1
    @WChargin sudo nice apt-get runs nice as root and will allow setting lower priorities than 0. apt-get will still run as root. nice sudo apt-get will run nice as the current user, and thus allow only priorities higher than or equal 0. apt-get will be started as root. – s3lph Jan 2 '15 at 13:31
  • 1
    @David I added ionice to my answer. – s3lph Jan 2 '15 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.