What is the nicest a Unix command can be?
The full command you want is:
chrt -b 0 nice -n 19 ionice -c 2 -n 7 [command]
The chrt
command at the beginning will switch things to the batch scheduling class, which is equivalent to adding 0.5 to the nice value. The -n option for ionice is a simple priority for the realtime (-c 1
) and best-effort (-c 2
) options, with lower values being higher priority just like nice values (but in the range 0-7). However, the ionice command is not strictly necessary, since I/O scheduling class and priority are by default derived from the CPU scheduling parameters, and nice -n 19
implies ionice -c 2 -n 7
.
However, you can get the absolute minimal resource usage by setting both the CPU and I/O scheduling classes to idle. In both cases, the 'idle' schedulers aren't actually idle schedulers, and you will still be able to use resources, it's just that everything will have higher priority.
For the CPU scheduling class, this also uses the chrt
command, albeit without needing nice
(priority must be set to 0
in the idle scheduling class), and looks like this:
chrt -i 0 {command or PID}
The nice
command on Linux mirrors the SVR4 version, which means that it can't change scheduling class, only nice value (which also behaves differently on Linux than classical UNIX, but that's a bit OT). As the original alternative scheduling classes were the POSIX.1E realtime SCHED_RR
and SCHED_FIFO
, the command to set scheduling classes ended up being called chrt
. The -i
option specifies to use the SCHED_IDLE
scheduling class
For the I/O scheduling class, you use ionice
. The exact command looks like this:
ionice -c 3 {command or PID}
The -c
option specifies what scheduling class to use, and 3 is the number for the idle class. Note that depending on which block I/O scheduler is being used, this may not actually impact anything. In particular, the noop
I/O scheduler doesn't support priorities or scheduling classes at all, and I'm pretty sure the deadline
schedulers (both the legacy one, and the blk-mq one) don't either.
If you want to do this programmatically, either for your own program, or to adjust things for other processes, check out the man pages for the sched_setscheduler
and ioprio_set
system calls (although both are worth reading if you just want more background too).