Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 226System · Performance

How to change the priority of a process

A backup or a big tar job runs during the day and the web app gets slow. You do not want to stop the job — only make it wait when others need the CPU or the disk. The nice value (−20 = highest priority, 19 = lowest) tells the CPU scheduler who goes first; ionice does the same for disk access; for services, systemd has Nice=, CPUWeight= and IOWeight=. This how-to uses all of them.

  • 5 Lessen
  • 5 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

226.1

The quick answer

nice -n 15 tar czf /backup/home.tgz /home
ps -o pid,ni,pcpu,comm -p 4711
renice -n 15 -p 4711
ionice -c 3 -p 4711

226.2

Step by step

  1. Start a job with low priority (line 1) — nice -n 15 COMMAND. Normal processes have nice 0.
  2. See the nice value (line 2) — the NI column (also in top).
  3. Change a running process (line 3) — renice -n 15 -p PID; a whole user: renice -n 10 -u backup. Normal users may only make their own processes nicer (higher number); lowering it needs root.
  4. Disk priority (line 4) — ionice -c 3 = idle class: the process uses the disk only when nobody else does. Start a job with both: nice -n 15 ionice -c 3 rsync -a /data /backup. (ionice works with the BFQ and CFQ I/O schedulers; with none/mq-deadline use systemd's IOWeight or cgroup limits instead.)
  5. For services — in a systemctl edit backup.service override:
override.conf
[Service]
Nice=15
CPUWeight=20
IOWeight=20

CPUWeight (default 100) shares CPU between cgroups under load; it works for the whole service with all its child processes.

  1. Nice does not limit — a nice-19 process still uses 100 % CPU if nobody else needs it. For a hard limit use CPUQuota=50% (systemd) or container limits.
  2. Do not make important things "less nice" by habit — negative values for one app can starve the system (SSH, monitoring).

226.3

Check that it worked

ps -o pid,ni,comm -p PID shows NI 15; under load top shows the web app getting most of the CPU while the backup still progresses; systemctl show backup.service -p Nice -p CPUWeight shows the new values.

226.4

Common problems

ProblemCauseFix
renice: failed to set priority … Permission deniedLowering the nice value as a normal user.Use root, or only raise the value.
No visible effectThe CPU is not busy, or the bottleneck is disk/memory.Nice only matters under CPU contention; use ionice/IOWeight for disk.
ionice has no effectI/O scheduler ignores priorities.Check /sys/block/DEV/queue/scheduler; use IOWeight in systemd.
Child processes have the old valuerenice on one PID only.renice -g PGID or set it on the service.
Job still slows the appNice does not cap usage.CPUQuota= or run the job at night.
Value lost after restartSet with renice by hand.Put Nice= in the service unit.

226.5

Go further

1Which nice value has the highest priority?

−20 is the highest priority, 19 the lowest; normal processes start at 0.

2Does a high nice value limit how much CPU a process can use?

No — it only lets other processes go first; when the CPU is free, a nice process can still use all of it.

Alle commando's in dit hoofdstuk