brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: chrt -i 0 runs a Linux job only when the CPU has nothing better to do

Linux
~/tips/tip-linux-chrt-i-idle-priority

chrt -i 0 ./reindex.sh

Give a background job the idle scheduling class, lower than even nice 19.

nice -n 19 lowers the priority of a job, but it still gets a small share of CPU when the machine is busy. The idle scheduling policy goes one step further.

chrt -i 0 ./reindex.sh

-i selects SCHED_IDLE, and the 0 is the priority, which must be 0 for this policy. The sched(7) man page describes it as meant for running jobs at extremely low priority, lower even than nice 19. On a busy box, the job waits until other work is done; on a quiet box it still runs at full speed. That makes it a good fit for search indexing, thumbnail generation or checksum runs that may take as long as they need.

To check or change a running process:

chrt -p 1234
chrt -i -p 0 1234

There is also -b for SCHED_BATCH, which tells the scheduler the job is not interactive and may be woken up a bit less eagerly. It is milder than idle.

This only covers CPU time. A job that mostly reads and writes the disk can still slow everything down, so combine it with ionice -c3 when disk load is the real problem.

There is a trap with idle jobs that hold locks. If such a job takes a lock that a normal process needs and then never gets CPU time because the box is busy, the normal process waits too. I would not use it for jobs that lock a shared database or file that other programs need.

Further reading: chrt(1) manual page, sched(7) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link