08
Jan
2015

Linux: Process priorities (aka, niceness)

Some of my friends reported that the Insurgency Dedicated Server  was running fine, then stuttered a load, then was fine again. I figured that this was due to another process running from a cron job (scheduled command), such as Munin. I decided that I was going to change the process priority of the Insurgency executable. In Linux, you use the nice command:

nice -n [value] command

The “niceness” value can range from -20 to +20. Positive numbers mean that the process is nicer, therefore lower priority and negative numbers make the process less nice, therefore higher priority.

As of now, a normal user can set a process to have lower priority:

nice -n 10 ps -l

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 18836 18835 0 80 0 – 5650 wait pts/0 00:00:00 bash
0 R 1000 20914 18836 0 90 10 – 2507 – pts/0 00:00:00 ps

As you can see, the ps process was launched with niceness of +10. But if we run nice with a niceness of -10, we get:

nice -n -10 ps -l

nice: cannot set niceness: Permission denied
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 18836 18835 0 80 0 – 5650 wait pts/0 00:00:00 bash
0 R 1000 21196 18836 0 80 0 – 2507 – pts/0 00:00:00 ps

And the niceness wasn’t changed from the default 0 niceness. So with our current setup, only the root user can change the niceness to a higher priority:

sudo nice -n -10 ps -l

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 22247 18836 0 80 0 – 14693 poll_s pts/0 00:00:00 sudo
4 R 0 22248 22247 0 70 -10 – 2507 – pts/0 00:00:00 ps

So now the niceness was set to -10. But now, the process will get launched as the root user (UID 0), which is definitely not recommended. To run a higher priority process as a normal user, add su [username] -c after setting the nice level as the superuser:

sudo nice -n -10 su [username] -c "ps -l"

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
5 S 1000 18835 18733 0 80 0 – 24886 ? ? 00:00:00 sshd
4 R 1000 22972 22971 0 70 -10 – 2507 – ? 00:00:00 ps

Now the process has been launched as a normal user (UID 1000) at a lower niceness (higher priority)! You can also use renice to change the priority on the fly if you wanted.

Ubuntu 14.10: Xscreensaver very old?
Problems with the iOS WordPress app

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.