Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know that niceness values are inherited from the niceness value of the parent process, but can I globally change the default niceness value for a particular user (myself).

In this case I have a small convenience server in mind, which is exclusively accessed via ssh. So, I think I could change my default shell from /bin/bash to /etc/bash5 while /etc/bash5 is this script:

#!/bin/sh
nice -n 5 /bin/bash

#file privileges: root:root 755

This strikes me as a terrible hack and error prone. There must be a better way.

I'm mostly interested in general solutions, that would also apply to a desktop/laptop system.


Edit: I tried the suggested limits.conf change, but it doesn't work as expected:

root@server# addgroup nice
root@server# adduser myself nice
root@server# echo '@nice soft nice 5' >> /etc/security/limits.conf

Then, from my client machine, I say

myself@client$ ssh server
myself@server$ sleep 1h &
myself@server$ htop

Now, the sleep process has an initial niceness value of 0, but if I change the value with F8 to 19 and then try to reduce it again with F7 it stops at 5.

Edit2: Solution

Instead of using the nice item in limits.conf, you actually have to use priority although it is counter-intuitive.

@nice soft priority 5
share|improve this question
    
Maybe put a renice -n 5 in your ~/.profile? –  jw013 Aug 1 '12 at 18:00
    
@jw013: That's a better idea than my bash5 approach, I give you this. But it still breaks if I say ssh myserver -- somecommand doesn't it? –  bitmask Aug 1 '12 at 18:02
    
@bitmask. I cannot reproduce your result for limits.conf. What's your output for ps -o pid,nice,cmd? –  donothingsuccessfully Aug 3 '12 at 8:40
    
Please post solutions to your own problems as answers, rather than edits to your question. Thanks! (It's okay to provide your own answer to your own question.) –  mattdm Aug 3 '12 at 19:14
    
On another note: this isn't what you asked, but you may find that cgroups provide more effective way of doing what (reduced priority for a certain user) you want than nice. –  mattdm Aug 3 '12 at 19:16
show 2 more comments

2 Answers 2

up vote 3 down vote accepted

You can set the priority for a particular user in /etc/security/limits.conf file.

root       hard/soft     priority    10

This way u can set hard or soft limit for any particular user. So all the processes which this root user will start will have 5 as the default priority vale.

According to Wikipedia page:

The exact mathematical effect of setting a particular niceness value for a process depends on the details of how the scheduler is designed on that implementation of Unix.

Here is the snapshot which show the above procedure works:

This image is the screenshot of /etc/security/limits.conf file

This is snapshot of limits.conf file which show a line which i added at last like

root     hard     priority    15

After changing this, I started one ssh session using the command ssh root@localhost

enter image description here

This screenshot show last two lines which shows the nice value of last two processes -bash and sshd: root@pts/3 to be 15.

Edit

Here is the snapshot which shows that u can increase and decrease the niceness of a process enter image description here

Edit 2:

Here is the snapshort which shows that even normal user can change the niceness value.

enter image description here

share|improve this answer
    
I was talking about niceness, not priority. And as I said on the other answer, it doesn't seem to be accepted. Do I have to restart some daemon, or something? –  bitmask Aug 1 '12 at 19:00
    
@bitmask: this one is working in mine...I am going to add snapshot in my answer wait... –  pradeepchhetri Aug 1 '12 at 19:20
    
@bitmask: I have updated the answer with the snapshots..:) –  pradeepchhetri Aug 1 '12 at 19:35
    
There is a difference between niceness and priority. I totally buy that you can change the hard priority, but I want to change the soft niceness. That's a different thing. –  bitmask Aug 1 '12 at 19:38
    
@bitmask: Accirind to wiki page, A niceness of −20 is the highest priority and 19 or 20 is the lowest priority. So first u have to understand that niceness and priority are same..And I want to clear that these are hard and soft limits but its not like hard priority and soft niceness.. –  pradeepchhetri Aug 1 '12 at 19:52
show 9 more comments

It looks like you can set a default priority for a user in limits.conf, with a line like:

username    soft  priority  5

See man limits.conf.

share|improve this answer
    
Sounds great. I just gave it a shot (using a special group though) @nice soft nice 5 but sshing into the machine and executing sleep 1h shows as having niceness 0 in htop. Any clue why the setting wasn't accepted? I'm on debian squeeze. –  bitmask Aug 1 '12 at 18:46
    
what is @nice in the above comment..it should be the one of the user of ur linux machine. –  pradeepchhetri Aug 1 '12 at 19:02
    
@pradeepchhetri: See the edit of my OP, please. It's a group name. –  bitmask Aug 1 '12 at 19:10
    
why @ in front of group name while putting an entry in limits.conf file ? –  pradeepchhetri Aug 1 '12 at 19:15
    
@pradeepchhetri: Because the man page says so? –  bitmask Aug 1 '12 at 19:25
show 10 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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