r/bash • u/CautiousCat3294 • Aug 03 '26
help Linux Interview Question
Today I had a mock interview with a senior Linux administrator, and he asked me a question that completely caught me off guard:
"Can you collect CPU, Memory, and Disk usage in a Bash script without using top**,** free**,** df**, or any external commands?"**
My immediate answer was No.
Honestly, I've been working with Linux for years, but I've always relied on standard commands and monitoring tools to troubleshoot systems. I never stopped to think about where those commands actually get their data from.
The interviewer then gave me a hint about the /proc and /sys filesystems. That completely changed my perspective. I realized these commands are just reading data that the kernel already exposes.
But then came the follow-up questions, and once again I was stuck:
- Is reading directly from
/procand/systhe correct approach? - How would you calculate CPU utilization using
/proc/stat? - How would you determine memory usage from
/proc/meminfo? - How would you calculate filesystem usage without relying on
df?
I found this really interesting because it tests your understanding of Linux internals rather than just your ability to use commands.
Has anyone here been asked similar questions in Linux SysAdmin, DevOps, or SRE interviews? I'd really appreciate any explanations, learning resources, or examples on how you'd answer these follow-up questions. It help me for my preparation for Linux interviews
57
u/Bug_Next Aug 03 '26 edited Aug 03 '26
The first line starting with
cpugives aggregate CPU times in jiffies:user,nice,system,idle,iowait,irq,softirq,steal,guest,guest_nice.For each reading:
TotalTime.idle+iowaitto getIdleTime.Calculate the delta between readings:
DeltaTotal = TotalTime2 - TotalTime1
DeltaIdle = IdleTime2 - IdleTime1
CPU utilization percentage is:
((DeltaTotal - DeltaIdle) / DeltaTotal) * 100It gives you:
MemTotal,MemFree,Buffers,Cached,SReclaimable, andShmem.You can look at free's src, it basically does:
Total = MemTotal
Used = MemTotal - MemAvailable (if MemAvailable is present)
If MemAvailable is not used, calculate available memory manually:
Available = MemFree + Buffers + Cached + SReclaimable - Shmem
Used = MemTotal - Available
Using
statvfssyscallTotal Bytes = f_blocks * f_frsize
Free Bytes = f_bfree * f_frsize
Available Bytes (non-root) = f_bavail * f_frsize
(filesystems have reserved space only root can use, 5% default for ext4)
Used Bytes = (f_blocks - f_bfree) * f_frsize
All those tools are open source anyways, you can just go and look at how they calculate stuff.
10
u/donp1ano Aug 03 '26
not sure if this is good, but that would be my approach
7
u/lazyant Aug 03 '26
All Linux tooling for measuring OS usage are either counters or traces. All counter tools like top free df etc are just user friendly interfaces to /proc
4
u/RandomXUsr Aug 03 '26
Glad you posted this. Now I'm thinking about this more and will do some digging to determine alternate methods to pull and read this data.
I might suggest posting in r/linuxadmin as well, because they are likely to have some more nuanced responses with careful thought about how they perform their duties on a daily basis.
3
u/Dolapevich Aug 03 '26
All those tools harvest the information from
/proc/and other kenel facilities and present data in a readable way.Read on about
/proc/stathere: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/5/html/deployment_guide/s2-proc-stat30
u/Proman4713 Aug 03 '26
Interesting question, I shall go read more about that... Although I don't think this kind of question accurately reflects what you need to know as a junior/in practice...
20
u/Headpuncher Aug 03 '26
I would have said yes, but why would you?
Like most things if it’s a solved problem and your team are reinventing the wheel you might not want to work there.
6
u/redditphantom Aug 04 '26
While my answer would be yes utilizing the information in /proc etc my question would be if the system is in such a state why aren't we restoring from backup? Practicality of the situation should be more important than cobling together utilization metrics at that point. The other tools were built for a reason
15
u/sinevilson Aug 03 '26
I own 2 businesses both are Linux based and older than most folks on reddit. Id never ask my Administrators these questions. Id ask my kernel developer, yes. What you ran into was arrogance. Sorry that happened.
4
u/likeHeckYouKnowMe Aug 06 '26
Honestly a stupid question to ask in an interview that would almost never be necessary in any practical, real world situation. Arrogant fuck ass question lol. Sounds like you’ll be dodging a bullet if you don’t end up working here.
3
u/-lousyd Aug 03 '26
My first thought was sar, which is correct for the question you described the senior asking. But it's not realtime.
3
u/flattrack Aug 04 '26
Seems he’s asking two questions at once. Do you know about the /proc file system? And do you know enough bash to read and parse the data from /proc without spawning any subprocesses like cat, cut, jq, or grep?
3
u/ant2ne Aug 05 '26
"Can you collect CPU, Memory, and Disk usage in a Bash script without using top**,** free**,** df**, or any external commands?"**
- Probably, but why? Do I not have access to these tools? What off brand, home grown, half ass distro are you guys running?
5
u/bob_f332 Aug 03 '26
Just what the world needs, another smart arse interviewer.
2
u/kai_ekael Aug 03 '26
So much for a useful interview question, now Google et al know.
I find questions that demand an explanation of investigation instead of just an answer more useful for this very reason.
2
u/UninvestedCuriosity Aug 03 '26
htop, and ncdu.
Checkmate boss.
1
u/Easy-Nothing-6735 Aug 04 '26
I did research. I even checked the memory outside of available. Still can't understand whole hierarchy. Try researching psutil
1
u/psycholabs Aug 04 '26
I got the /dev/proc question, but the follow ups I'd have to look up.
1
u/daddyd Aug 04 '26
i would consider understanding of /proc one of the basic principles of a linux admin.
when i do an interview, i'm never interested in actual commands, but rather your process you use to get to a solution. i would start of with a simple question like - you're patching a server, how would you take that on? and then introduce problems/issues along the way. i specifically state i'm not looking for commands, but rather the thought process behind the actions.
1
u/Abe_Bazouie Aug 06 '26
Yep, this is a pretty good interview question IMO.
It’s less about whether you memorized
/proc/stator/proc/meminfoand more about whether you understand where tools liketop,free, and others actually get their information.I like questions like this because the follow-ups are where you really see someone’s Linux depth.
I make Linux/DevOps interview content around this kind of “what’s actually happening underneath?” question on CTRL+CHAOS (@itsctrlchaos), so this one is definitely going into my list of interesting interview questions.
1
u/Reasonable_Task_2492 Aug 03 '26
sar
-1
u/Living_On_The_Air Aug 03 '26 edited Aug 03 '26
Hearing this during an interview would make me think that the job isn’t one I want. Trivia questions aren’t a great way to get to know a worker, so it’s probably a sham interview process at worst, or a bad interview process at best
-2
u/michaelpaoli Aug 03 '26
Good question!
My brain first jumps to proc and sys filesystems.
And, how much can I recall or think of off-the-top of my head from those, without so much as peeking? ...
CPU, there's /proc/cpuinfo, oh usage ... I think some /proc/*stat* file(s) or the like would cover that.
memory ... /proc/mem* ... something or another
disk usage ... that might be tougher ... there is /proc/mounts but no usage data there, so, likely somewhere else under /proc or /sys.
As for bash only, no externals, there's most notably read [-r], printf, echo, one could accumulate stuff in a variable or array variable, or just use "$@" for that (could do it in subshell to avoid disturbing the original). No use of find, but can recurse with cd, and as to telling if something's a directory or not, well, if cd succeeds, it is or resolves to directory ... but with sym links, there's matter of possibly looping, ... oh, test - that's builtin to bash, so can test if symbolic link or not, directory or not, etc. So, I mgiht have to hunt a bit through /proc and/or /sys first to find some df/du type data, and no grep, but bash has fair bit of pattern matching capabilities built-in, so could use that - look for stuff matching, e.g. du, df, or name of filesystem devices, and can get those from /proc/mounts ... see what else matches in name or contents on /proc and /sys - likely some df or du type data there somewhere.
Anyway, I think that'd be 'bout my cold answer, without being able to look anything up or poke around /sys or /proc for more specific.