leo charre

contact links about resume

dev design near life experience heroes




giving priority to different applications under linux with nice

I want to talk about ‘nice’.

I’ve been using linux for years and I never paid attention to ‘nice’ until recently.
And it’s a beautiful feature.. really gorgeous, simple to use, incredibly valuable.

Basically what it does is tell the computer what priority different things have. In real time, or ahead of time.

In regards to user space..
What I mean here, is if you’re using your computer as workstation.
Imagine you have many things going on. And some of these things make your computer go slower- of course.
For example, if you’re using the fucked up firefox which eats your cpu. And you fire up also, pan, the newsgroup reader, and also.. you are resizing images. These are all potentially cpu hog activities.
And they will fight for your cpu and memory equally.

But it doesn’t have to be this way!
You can go in, once they are all running (the different processes), and tell the operating system (linux) that you want to give priority to something else.

Then you use ‘nice’. Nice is a setting from -20 to +20 that tells the operating system the priority of the thing.
When you start up things, open applications, etc- they start out with a default nice of 0.
If you set the nice to +15, for example- Then you lower the priority. That means, things you want in the background- should have a higher nice setting.
As for negative nice, only root can do that.

What is so very useful, is to be able to fire up a terminal, use top, and tell the machine that you want to lower the priority for some applications, so that the ones ou have open get allthe cpu and memory.

  1. SETTING NICE WITH TOP

    1. Fire up a terminal.
    2. Run the command # top.
      This is what you see:

      viewing pids in top

      This screen shows you the current running processes, etc.

    3. Now, let’s pick something running that we want to lower the priority for..
      press the ‘r’ key- this is for ‘renice’ (change the nice setting).

    4. It asks you what pid to change, look at the list of running processes on the right, and find the corresponding pid (process id) on the left.
      Type in that number.

    5. Now you are asked what to set it to, type in 15, for a lower priority.

    You’re done! That’s it, now … that process will only use up memory and cpu not being used by processes with lower nice value!! What’s so fucking cool about this, is that as your applications use less cpu and memory, the pids (applications) with lower nice setting will still come back and use free resources!

    So, if you have gimp and firefox running, and you set firefox to nice 13, and gimp to nice 12- If gimp asks for more mem and cpu, it will take it from firefox! And as soon as gimp stops using resources, firefox will take them back!

    Incredibly uber cool!

  2. SETTING NICE WHEN AN APPLICATION IS GOING TO RUN

    A more involved example, owering priority for updatedb

    Now, I love my slocate.. my locate db.. This tends to update daily by cron setting- (automatically once a day).
    But it takes some of my cpu and mem away, I really don’t care if ‘updatedb’ takes two minutes or twenty to run.
    I just get super fucking annoyed when my cpu slows up because updatedb just got called up behind my back..

    So.. let’s make sure it fires up with a nice setting of 19, which is very low priority..

    Now, there’s usually a setting in some crontab or something.. that calls the command /updatedb’.
    In what file is it? Depending on your setup and distro, it could be in various places.
    Let’s find it this way…

    $ find /etc/ -name "*cron*" -type f | xargs grep updatedb

    What’s going on?

      We said to find all files with the name “*cron*”, so /etc/crontab /etc/cron.daily/cron would match.

    • Furthermore, we piped the output (what you would normally see printed to screen) to a command to search the inside of those files and match the text string ‘updatedb’.

    This will show us where and if there’s a cron file calling the command ‘updatedb’.
    Make sure that the command calling updatedb, is preceded by:

    nice -n 19 updatedb...

    Imagining you did not have a crontab entry for updatedb.
    Here’s what you would do, as root..

    • become root
    • # vim /etc/crontab
    • Make an entry that says:
      15 07 * * * nice -n 19 updatedb 
    • :qw save and exit..
    • Make sure to tell crontab to load the settings..
      # crontab /etc/crontab

    • Done.

    Now everyday at 7am, updatedb will run with a low priority, so if the computer is being used, it will surrender resources as needed.

    A simpler example, starting a command with a low priority

    Of course, you can start your processes, applications with a low setting for nice.
    What if you want to find all images larger than 1 megabyte and resize them, while you do something else ?
    Normally, mogrify (imagemagick) would eat your cpu alive.
    But not with nice!!!

    The normal command would be:

    $ find ~/images -size +1M -exec mogrify -resize '800x600>' -quality 90 '{}' \;

    Let’s make sure that both find and the spawned ‘mogrify’ calls are set to a low priority, the command modified would be:

    $ nice -n 15 find ~/images -size +1M -exec mogrify -resize '800x600>' -quality 90 '{}' \;

So, those are just workstation examples.
What’s very cool, is if you code or manage applications to run in heavy servers that get a lot of use- which I do.
And you want to manage stuff..
I have this one server with a loooong list of crontab entries.
Users may log into this machine at any moment- and I want to make sure that some of the things I run on cron don’t slow them down.
So I set a nice -n X setting for those crontab jobs. Super cool.

In closing

This information is not authoritative! It’s an introduction. It is meant for you to be curious about unix nice.
Consider looking at the manual… Try ‘$ man nice’.


Linux User