2

I've read about nice renice and ionice to control the scheduling priority of running processes. However, I still don't understand when and how are they useful for. For instance doing something like:

nice -n 13 pico myfile.txt

3 Answers3

5

With regards to pico it's unlikely to ever be much cause for concern so it's not the kind of command you would normally nice/renice or ionice. However your command would execute pico niced to +13. Which would mean it would get significantly less time allocated to it. For example, following on from your pico line. Normally pico executed looks like this:

UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S     ADDR TTY           TIME CMD
501 20118 20117     4006   0  31  0  2435548   1068 -      S     eb577e0 ttys000    0:00.03 -bash
501 20136 20118     4006   0  31  0  2434992    772 -      S+    85eed20 ttys000    0:00.00 pico

Where NI is the nice level. If I run pico with your command it looks like this:

UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S     ADDR TTY           TIME CMD
501 20118 20117     4006   0  31  0  2435548   1068 -      S     eb577e0 ttys000    0:00.03 -bash
501 20179 20118     4006   0  18 13  2434992    904 -      SN+   85eed20 ttys000    0:00.01 pico

OK so that demonstrates the command line you run is effective, but what does it actually do? OK let's say you have a system that is quite busy but not obscenely so. It's pretty busy all around the clock, at midnight an essential maintenance task starts. It's mysql command line running a script to generate some daily stats. There's a lot of processing so it hits the CPU hard and the users complain about this. Vehemently, as they do. What can you do about it? It's not essential how long this script takes so you prepend 'nice -n 13' to the command and the next time it runs it doesn't affect the users so bad but takes a bit longer to complete. The owner of the MySQL script doesn't care as he only wants the stats when he gets in the next day. Everyone is happy.

Stories aside these tools can let you exert some control over how the system prioritises the CPU (possibly IO in the case of ionice) time allocated to tasks. They will only have an effect when the system is loaded, before that everyone tasks are being allocated all the time they need.

The renice command works in a very similar way but it allows you to alter the priority of processes that are already running. So in the above anecdote you could use renice to alter the priority of the mysql command without restarting it. The following command would renice all running tasks named 'mysql' to +13, a much lower priority:

renice -n 13 -p `pidof mysql`

As for ionice I cannot comment but would guess it lets you do similar things with IO. Influencing how much io certain tasks get to perform, e.g. disk read/writes. Could be useful in throttling backup process if necessary?

Diziet
  • 188
  • 4
2

ionice is an interface to the IO scheduler subsystem of the Linux kernel, it allows for setting IO priorities on a per process basis. The use-case of that would be something like reducing the priority of the updatedb command that builds the database for the locate command in a way that other IO is running undisturbed as updatedb only gets to do IO when no other IOs are currently running.

pfo
  • 5,630
  • 23
  • 36
0

nice is to start a program with adjusted niceness level which is basically the CPU scheduler. Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process). Default value is 10 and only root can use negative adjustments to have more favorable priorities.

renice does the same as above only to already running processes by specifying process ids (option -p), process group ids (option -g) or usernames or user ids (option -u).

ionice sets or adjusts the I/O scheduler niceness of programs and processes for access to "disk" devices. The priority lies within one of three schedule classes: 1 or realtime (tries to give priority no matter what's already going on), 2 or best-effort (normal scheduling, see also reference link), 3 or idle (scheduled only when no other process is accessing disks).

For example, you can run a fstrim command at boot on rc.local with such a snippet in order to have your job done with little burden to the whole system.

(
  sleep 15s #1
  /usr/bin/fstrim -a -v &> /var/log/fstrim.log & #2
  pid=$! #3
  ionice -c 3 -p $pid #4
  renice -n +19 -p $pid #5
) &> /dev/null & #6
  1. It waits for 15 seconds before proceeding to avoid overloading the system immediately (so it can finish its boot up sequence faster).
  2. It starts the fstrim command in the background with its output in a log file and
  3. it records its PID number.
  4. It adjusts the process I/O scheduler to the idle class and
  5. set it to the lowest CPU scheduling priority.
  6. The whole snippet is started in the background to let the boot process go on.

References.

EnzoR
  • 282
  • 3
  • 10