How to make all the processes start with niceness 5

nicesettingsusers

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

Best Answer

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

Related Question