Managing a job's priority

Starting a job with a low priority

It is usually simplest to start jobs with a priority of 19. Use:

/bin/nice -n 19 command &

where command is the command and arguments you will use to run the job. You can check the priority of your job with either top or ps. See below for details of these commands. For example to run a program called ./rmean in the current directory using a data file mydata you would use:

/bin/nice -n 19 ./rmean &

Note that the version of nice you use depends on the shell (usually either bash or csh) that you are using. Users of the bash shell will use /bin/nice, csh and tcsh users use a shell built-in version. To find out which version you are using enter:

which nice 

Changing the priority of a running job

There are two simple ways to change the priority of a running job.

Using top
Start top with

top -u username

where username is replaced by your own username. You should then see something like:

top - 10:30:17 up 88 days, 16:05, 4 users, load average: 1.68, 1.85, 1.80
Tasks: 102 total, 1 running, 101 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0% us, 0.7% sy, 0.0% ni, 49.9% id, 48.8% wa, 0.2% hi, 0.5% si
Mem:   2056680k total, 2045512k used,   11168k free,  1008k buffers
Swap: 10474372k total, 3110524k used, 7363848k free, 18376k cached

PID   USER     PR NI  VIRT  RES SHR S %CPU %MEM TIME+   COMMAND
5351  hutchins 16  0  8384 1120 848 R  0.3  0.1 0:00.02 top
25328 hutchins 16  0 48480  936 736 S  0.0  0.0 0:00.05 sh
5348  hutchins 16  0 44996  532 444 S  0.0  0.0 0:00.00 rmean

The program updates every three seconds by default so you should see minor changes to the values in bold. The top half of the display gives information about system-wide resource usage. The lower part shows information about processes owned by the user. The line beginning PID shows headers describing the contents of each column.

Look at column 4 in the lower part of the display. It is headed by NI and displays the nice value of all the processes owned by user hutchins. At the moment all are 0. The command that each process is running is shown in the last field. We are going to change the nice value of the process running the rmean command.

To enter a command in top just enter letters in the line above the PID line. Enter:

r

The display should now look like this [system display information omitted to make the detail clearer.]

PID to renice
PID   USER     PR NI  VIRT  RES SHR S %CPU %MEM TIME+   COMMAND
25328 hutchins 16  0 48480  912 716 S  0.0  0.1 0:00.05 sh
5348  hutchins 16  0 44996  520 432 S  0.0  0.0 0:00.00 rmean
5351  hutchins 16  0  8384 1120 848 R  0.0  0.1 0:00.70 top

Enter the PID of the rmean command which is displayed in column 1:

5348

The display changes to:

Renice PID 5348 to value:
PID   USER     PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
25328 hutchins 16  0 48480 912 716 S 0.0 0.1 0:00.05 sh
5348  hutchins 16  0 44996 520 432 S 0.0 0.0 0:00.00 rmean
5351  hutchins 16  0  8384 1120 848 R 0.0 0.1 0:00.70 top

Enter the new nice value:

19

Now look at the value in the NI column for command rmean

PID   USER     PR NI  VIRT RES SHR S %CPU %MEM TIME+   COMMAND
5351  hutchins 16  0  8384 964 764 R  0.0  0.0 0:00.71 top
25328 hutchins 16  0 48480 584 452 S  0.0  0.0 0:00.05 sh
5348  hutchins 16 19 44996 532 432 S  0.0  0.0 0:00.00 rmean

To leave top enter:

Using ps and renice
It is also possible to use:

ps -o pid,nice,comm -u username

which lists the current commands that you are running, their process identifier and the nice value. Remember to replace username with your username.

  PID NI COMMAND
25328  0 sh
  5654  0 rmean
  5675  0 ps

Then use the renice command to change the value:

renice 19 5654

which reports:

5654: old priority 0, new priority 19

To check that we have changed the priority use:

ps -o pid,nice,comm -u username

again and you should see:

  PID NI COMMAND
25328  0 sh
  5654 19 rmean
  5675  0 ps