# Being Nice - Shell Scripting

In a multitasking operating system (which Linux is), the kernel is responsible for assigning CPU time for each process running on the system. Only one process at a time can actually be running in the CPU, so the kernel assigns CPU time to each process in turn.

By default, all processes started from the shell have the same scheduling priority on the Linux system. The scheduling priority is the amount of CPU time the kernel assigns to the process relative to the other processes.

The scheduling priority is an integer value, from −20 (the highest priority) to +20 (the lowest priority). By default, the bash shell starts all processes with a priority of 0.

This means that a simple script that only requires a little bit of processing time gets the same CPU time slices as a complex mathematical algorithm that can take hours to run.

Sometimes you want to change the priority of a specific command, either lowering its priority so that it doesn’t take as much processing power from the CPU or giving it a higher priority so that it gets more processing time. You can do this by using the nice command.

The nice command

The nice command allows you to set the scheduling priority of a command as you start it. To make a command run with less priority, just use the -n command line option for nice to specify a new priority level:

$nice -n 10 ./test4 test4out & [1] 29476$ ps al
F UID PID PPID PRI NI WCHAN STAT TTY TIME COMMAND
100 501 29459 29458 12 0 wait4 S pts/0 0:00 -bash
000 501 29476 29459 15 10 wait4 SN pts/0 0:00 /bin/bash .
000 501 29490 29476 15 10 nanosl SN pts/0 0:00 sleep 10
000 501 29491 29459 14 0 - R pts/0 0:00 ps al
$The nice command causes the script to run at a lower priority. However, if you try to increase the priority of one of your commands, you might be in for a surprise:$ nice -n -10 ./test4 test4out
[1] 29501
$nice: cannot set priority: Permission denied [1]+ Exit 1 nice -n -10 ./test4 >test4out$

The nice command prevents normal system users from increasing the priority of their commands. This is a safety feature to prevent a user from starting all of his or her commands as high priority.

The renice command

Sometimes you’d like to change the priority of a command that’s already running on the system. That’s what the renice command is for. It allows you to specify the PID of a running process to change the priority of:

$./test4 test4out [1] 29504$ ps al
F UID PID PPID PRI NI WCHAN STAT TTY TIME COMMAND
100 501 29459 29458 12 0 wait4 S pts/0 0:00 -bash
000 501 29504 29459 9 0 wait4 S pts/0 0:00 /bin/bash .
000 501 29518 29504 9 0 nanosl S pts/0 0:00 sleep 10
000 501 29519 29459 14 0 - R pts/0 0:00 ps al
$renice 10 -p 29504 29504: old priority 0, new priority 10$ ps al
F UID PID PPID PRI NI WCHAN STAT TTY TIME COMMAND
100 501 29459 29458 16 0 wait4 S pts/0 0:00 -bash
000 501 29504 29459 14 10 wait4 SN pts/0 0:00 /bin/bash .
000 501 29535 29504 9 0 nanosl S pts/0 0:00 sleep 10
000 501 29537 29459 14 0 - R pts/0 0:00 ps al
\$

The renice command automatically updates the scheduling priority of the running process. Just as with the nice command, the renice command has some limitations:

• You can only renice processes that you own.
• You can only renice your processes to a lower priority.
• The root user can renice any process to any priority.

If you want to fully control running processes, you must be logged in as the root account.

Login

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd

Shell Scripting Topics