Can Bash collect CPU, memory and disk usage without external commands?

November 16, 2026 5 answers
Asked by c.591~sparecrank

I came across an interview question about collecting these metrics in Bash without top, free, df or any other external commands. Is reading /proc and /sys enough, and how would the calculations work?

Answers

_Juvren+8c

For CPU, read the aggregate cpu line at the start of /proc/stat twice, with some time between samples. The numbers are cumulative CPU time counters, so one reading alone doesn't give you utilization.

For each sample, add user, nice, system, idle, iowait, irq, softirq and steal to get a total. Add idle and iowait for an idle total. Then calculate 100 times (delta total minus delta idle) divided by delta total. Bash arithmetic can do that with integer precision.

Don't add guest and guest_nice again. They're already included in user and nice. Also, treating iowait as idle is a convention, so say which definition you're using.

9b~claspturner_407:

With no external commands, Bash can wait between samples using the timeout option of its read builtin. Check that the total delta is greater than zero before dividing.

Reply to this answer
_r51!foldnotch.6

Memory is the easier one. Read /proc/meminfo with a Bash while read loop and pick out MemTotal and MemAvailable. An approximate used percentage is 100 times (MemTotal minus MemAvailable) divided by MemTotal.

That gives you a useful estimate of memory not readily available for new work, rather than treating all cache as permanently used. The values are reported in kB, so the units cancel out for the percentage.

Reply to this answer
8k!modcrank_274

Filesystem usage is the awkward part of this question. /proc can tell you about mounted filesystems, but it doesn't generally provide the free and total space figures that df reports.

Those figures normally come from a filesystem statistics call such as statfs or statvfs. Bash has no builtin for that, so a strict answer with no external commands is that you can't generally calculate filesystem usage for an arbitrary mount. I'd clarify whether a helper program or another language is allowed.

c.591~sparecrank:

That distinction helps. So reading the mount information isn't the same thing as getting the filesystem's capacity and free space.

Reply to this answer
3h!indexbarrel_729

I'd also ask what they mean by disk usage. Space used on a filesystem and disk activity are different metrics. /proc/diskstats exposes device I/O counters, but those won't tell you how full a filesystem is.

Reply to this answer
4z~puddlejumper_63

As an interview exercise, this can be a useful way to talk through kernel interfaces and sampling. For an actual monitoring script, though, using established tools is usually more practical than recreating their behavior in Bash. I'd explain both the approach and the limits rather than pretending every metric is available from a text file.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions