renice Command in Linux


Back

The nice and renice command can be used to manage the priority of different processes.While the nice command is used to start a process with a different priority, the renice command is used to change the priority of a currently running process. Process priority in Linux specify numbers that seem counter-intuitive. The range of available nice numbers can vary from  -20 to 19. A process given a  priority of -20 takes precedence over all other processes. In contrast, a process given a priority of 19 will wait until the system is almost completely free before taking any resources. The default nice number of a process is 0.

# nice - 19 ./intensivescript.

This command starts the noted script with the lowest possible priority. If started at night ,the script is run untill just about any other jobs, such as run in one of the /etc/cron. directories is scheduled for execution. As such scripts are run on a schedule, they normally should take priority over some user-configured programs.

Sometimes a program is just taking up too many resources. If you need to make sure that program continues to run, one step before killing the associated process is to lower its priority with the renice command. 

Normally, the easiest way to identify a process that's taking up too many resources is by its PID in the output to the top command. That PID number is in the left-hand column of he output. For example,if you identify a process that's monopolizing current CPU and memory resources, copy the PID number of that process. If that number were 1234, the following command would change the nice number of that process to -10, which gives that process a higher priority than the default.

# renice -10 1234


Use ps axl to display the nice value of all running process as shown below.

# ps axl

How to assign a low priority to a shell-script? (higher nice value)

In the example below, when I started the nice-test.sh script in the background, it took the nice value of 0.

# ./nice-test.sh &
 [3] 13009

# ps axl | grep nice-test
0   509 13009 12863  17   0  4652  972 wait   S    pts/1      0:00 /bin/bash ./nice-test.sh
[Note: 6th column with value 0 is the nice.]

Now, let us execute the same shell script with a different nice value as shown below.

# nice -10 ./nice-test.sh &
[1] 13016

# ps axl | grep nice-test
0   509 13016 12863  30  10  4236  968 wait   SN   pts/1      0:00 /bin/bash ./nice-test.sh
[Note: 6th column with value 10 is the nice value for the shell-script.]

How to assign a high priority to a shell-script? (Lower nice value)

In the following example, let us assign a nice value of -10 (minus 10) to the nice-test.sh shellscript.

# nice --10 ./nice-test.sh &
[1] 13021

$ nice: cannot set priority: Permission denied

Note: Only root user can set a negative nice value. Login as root and try the same. Please note that there is a double dash before the 10 in the nice command below.

# nice --10 ./nice-test.sh &
[1] 13060

# ps axl | grep nice-test
4     0 13060 13024  10 -10  5388  964 wait   S<   pts/1      0:00 /bin/bash ./n