Thursday, October 18, 2007

Modify a Command's Scheduling Priority with nice

There may be times when you need to modify the priority of a command prior to starting it. You can do this by using the nice command. It modifies the scheduling priority of the command . 19 is the lowest priority, 1 is the highest priority, and 10 is the default. Here are a few examples.


Highest priority
# nice -n 1 myfind &
returns PID

Lowest priority
# nice -n 19 myfind &
returns PID

Default priority is 10

Also, the renice command can be used to change the priority of a job.

# renice -18 PID

stephane added the priorities range from -20 (or -19 depending on the Unix) to 19, -20/-19 being the most favorable scheduling, 19 the least favorable one. The negative increments are allowed only for root.

So if you are root, you can do this : nice -19 myfind

to get it running with max priority.

8 comments:

Mads said...

When you mention nice, perhaps you should also mention renice? as it allows you to set the priority of a running process.

Anonymous said...

Roy, I don't intend to be mean, and I suspect you are trying to help people: but almost every post you make shows that you don't fully understand the subject of your post. I hope it is clear to people that these are tips and hits from Roy, and as such may be useful when treated that way; but these are not the opinions of Sun Microsystems - a subtle difference that I know can confuse people.

Artem Nosulchik said...

cryoPID may be useful as well when you have to free some memory/cpu resources and don't want to lost current state of running process. Actually, cryoPID captures process' state into a file and restores it later when it's needed.

esofthub said...

Anonymous, there's absolutely no offense taken. These are just my personal tips and not reflections of any vendor whatsoever. As you said, these are just “tips and hits” from Roy. Yes, I know there is a lot left out when I post 3-4 sentences in each post but they are not meant to be all-inclusive by any stretch of the imagination. The commands are useful to me when I used them in specific way and might be useful to others. I realize the commands have many more capabilities than what I mentioned or ever could mention. But that type of discussion is well beyond the scope of a free personal blog. :) That level of discussion will cost time or money (live classes, web/CD courses, consultants, books, etc) or both.

When I provide a tip that needs to be expanded, normally knowledgeable readers will chime in. I always tell readers that I know or those who email me to read the comments or obtain detailed guidance from external sources. As I tell people in my disclaimer, please do your own research before trying the tips. When I read someone's tip, I do the research to apply to my own particular situation.

For readers that want more information about these commands, I would strongly suggest using the man pages (for whatever platform they are using), signup for a course or purchase a book (eBay is a great source) on the subject.

Again, I hope no one thinks this blog represents the opinions of any vendor. I certainly do NOT endorse any vendor and vice versa and I don't work for them. This blog just happens to be a representation of tasks performed at home or work. It's like a personal journal…and selfishly speaking, I returned to these posts for my own personal use from time to time. :)

Btw, I have received many emails thanking me for the tips (done in a specific way).

Good comment. I truly appreciate it. Thanks.

Anonymous said...

actually in solaris you should use priocntl, nice and renice are there only because of backwards compatibility

Stephane said...

hi roy !

I'd just add that the priorities range from -20 (or -19 depending on the Unix) to 19, -20/-19 being the most favorable scheduling, 19 the least favorable one. The negative increments are allowed only for root.

So if you are root, you can do this : nice -19 myfind

to get it running with max priority.

esofthub said...

Hey stephane! I hope you are doing well.

Thanks for dropping by and always adding an insightful comment or two...now if ux-admin would drop in from time to time. :)

esofthub said...

Thanks mads for adding to the post.