QT #3 - Background jobs that leave the server alone
A Quick Tip on keeping backups and other heavy jobs from slowing everything else down: nice for the CPU, ionice for the disk, a real test of what they do, and the one disk setting that makes ionice do nothing.
On my servers I always chase low load numbers, and the usual troublemakers are the jobs nobody is waiting for: the nightly backup, a big tar, a reindex. They're in no hurry, but left alone they take as much CPU and disk as they can get, and the website next to them gets slow.
Two old commands fix that by putting those jobs at the back of the line: nice for the CPU and ionice for the disk.
nice, for the CPU #
Every process has a niceness, from -20 to 19. The default is 0, and the higher the number, the more the process gives way to the others. Any user can raise it. Only root can lower it.
nice -n 19 tar -czf backup.tar.gz /srvThe part people often get wrong is what that number does. Niceness only matters when processes compete. On an idle machine, a job at 19 still gets the whole CPU. It just steps aside the moment something else wants it.
To see it, two endless loops pinned to the same CPU core with taskset, one normal and one at 19:

97.3% against 1.3%. The niced loop didn't stop. It just got whatever the other one left.
ionice, for the disk #
Disk is often the real bottleneck for backups, and nice doesn't touch it. ionice does. Its idle class, -c3, only gives a process disk time when nobody else has asked for any. The best-effort class with the lowest priority, -c2 -n7, is the softer option.
Both together, and a check that they took:
$ ionice -c3 nice -n19 tar -czf /tmp/docs.tar.gz /usr/share/doc & $ ionice -p $(pgrep -n tar) idle $ ps -o pid,ni,comm -p $(pgrep -n tar) PID NI COMMAND 474 19 tar
Source
$ ionice -c3 nice -n19 tar -czf /tmp/docs.tar.gz /usr/share/doc &
$ ionice -p $(pgrep -n tar)
idle
$ ps -o pid,ni,comm -p $(pgrep -n tar)
PID NI COMMAND
474 19 tarIn a crontab:
0 3 * * * ionice -c3 nice -n19 /usr/local/bin/backup.shAnd in a systemd service, the same thing is two lines in [Service]: Nice=19 and IOSchedulingClass=idle.
The catch with ionice #
ionice depends on the disk's I/O scheduler, and the Linux docs say only two of them honor priorities: bfq and mq-deadline. Fast NVMe drives are often set to none, and with none, ionice changes nothing. The active one is the one in brackets:
$ cat /sys/block/vda/queue/scheduler none [mq-deadline] kyber bfq
Source
$ cat /sys/block/vda/queue/scheduler
none [mq-deadline] kyber bfqThis disk uses mq-deadline, so the idle class works. On a disk showing [none], nice still helps with the CPU, and the disk side needs another tool, like the I/O limits of a systemd unit.
nice and ionice also do nothing for memory or network. A low-priority job that fills the RAM still drags everything down with it.