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: systemd-cgtop is top for systemd units instead of processes

Systemd
~/tips/tip-linux-systemd-cgtop-resource-by-unit

systemd-cgtop -c -n 1

Shows CPU, memory and I/O use grouped per systemd unit, sorted live.

top shows processes, but on a server I often want to know which service is behind the load. systemd-cgtop groups by control group, which maps to units.

Run it without arguments for a live view, or choose the sort order with an option:

systemd-cgtop -m
systemd-cgtop -c -n 1

The man page lists -c to order by CPU load, -m by memory usage, -i by disk I/O load, -t by number of tasks and -p by control group path. While it runs, the keys p, t, c, m and i switch the sort order the same way, and q quits. With -n 1 it performs one iteration and exits, which gives you a snapshot you can look at later or send to a file. Batch mode with -b does not accept input at all and runs until the iteration limit is reached or it is killed, so it suits a script:

systemd-cgtop -b -n 1 -m > /tmp/cgtop.txt

The refresh delay can be changed with -d, for example -d 5 for five seconds.

The caveat is accounting. The page says resource usage is only counted for control groups that have the right controllers turned on, and it recommends MemoryAccounting=1 and IOAccounting=1 in the unit files. Without them, no resource accounting is available for system services and the data is incomplete, so a service may show empty columns until you enable those settings and restart it.

Further reading: systemd-cgtop manual page

Going deeper: Keep a domain pointed at your home with Cloudflare DDNS on Unraid

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link