Comment on More believable for a Linux OS

<- View Parent
okamiueru@lemmy.world ⁨4⁩ ⁨months⁩ ago

I don’t think the Unix philosophy of having lots of small tools that do one thing and do it well that you compose together has ever been achieved

Why do you think this might be the case? It’s not remotely accurate, which suggests that you must understand it very differently than I do. To some extent, I am curious.

I’ll give you a recent example. Which is just from yesterday. I had a use case where some program had a memory leak, which would eventually lead to the system running out. So, I “built a program that would monitor this and kill the process that used the most memory”. I don’t know how complicated this is in windows and PS, but it took about 2 minutes in Linux, and it very much leverages the Unix philosophy.

Looks something like this:

get_current_available_memory_mb() {
    cat /proc/meminfo | grep MemAvailable | grep -oP '\d*' | xargs printf "%d / 1024  \n" | bc
}

Each of those pipes do their thing well. The PID of the process using the most memory you can get with something like:

ps aux --sort=-%mem | head -n2 | tail -n1 | awk '{print $2}

Again, using the same Unix philosophy. You then check if the available memory is below some threshold, and send a kill signal to the process if it does. Instead of adding an infinite loop in the script, you stop at making it do that one thing. That is, 1. check remaining memory. 2. if lower than X, kill PID". Let’s call this “foo.sh”. So you end up with is just:

watch -n 2 – ./foo.sh, and there you go. Every two seconds, it checks available free memory, and saves my system from freezing up.

If memory serves me correctly, PS also supports piping, so I would assume you could do similar things. Would be weird not to, given how powerful it is.

source
Sort:hotnewtop