8 ms·
ps aux written in bash without forking
- est 2y agoThat's cool, please support `ps axufww`, i miss this a lot on macOS
- oldandboring 2y agoI am so embarrassed that I came here to say this, and also quite pleased that you beat me to it :)
- deleted 2y ago[deleted]
- oxygen_crisis 2y ago> An interview question for a position that requires knowledge of bash/linux/stuff could be: > What if you're ssh'd into a machine, you're in your trusty bash shell, but unfortunately you cannot spawn any new processes because literally all other pids are taken. What do you do? I'd look in the /proc/[pid]/ filesystem for visibility into what processes are exhausting the PID space. `kill` is a shell builtin in bash, you don't have to rely on forking a new process like /bin/kill. If you can find out the parent process whose children are exhausting PIDs you're well on your way to stopping it and getting a handle on things again. And I'll be darned, this script parses /proc. No | pipes or $( .. ) substitutions that would need to spawn another bash subshell process either. Pretty clean.
- kstrauser 2y agoMy answer in an interview was “exec Python”. Then you can call all the posix functions you need without launching separate commands. This went over quite well.
- Onavo 2y agoIt's interesting that mainstream Unix shells do not have a syscall function. That would be very useful.
- layer8 2y agoWell, you can use https://github.com/taviso/ctypes.sh https://github.com/taviso/ctypes.sh.
- chx 2y agoInstall this on production to almost guarantee to hear from the author in his official capacity :P
- elashri 2y ago> Here is what people have been saying about ctypes.sh: "that's disgusting" "this has got to stop" "you've gone too far with this" "is this a joke?" "I never knew the c could stand for Cthulhu."
- fuzztester 2y agoThis made me giggle. And after the exec, if they asked me to parse a Python expression, I'd type "eval(expr)".
- fuzztester 2y agoOr even: import code code.interact() # https://docs.python.org/3/library/code.html https://docs.python.org/3/library/code.html
- 2y ago
- INTPenis 2y agoIzabera is one of the gurus in #bash@libera (formerly freenode). I love all those gurus. They've taught me so much over the past decade.
- CGamesPlay 2y agoLicense: > You can freely copy and use this in any interview in which you get asked that question. No other uses are ever allowed.
- deleted 2y ago[deleted]
- Arch-TK 2y agoYeah it's a joke license for a joke project. Do you want me to try to persuade her to dual license it with something more permissive?
- prmoustache 2y agoWhy would we need you to do that?
- Arch-TK 2y agoI live with her :P
- ykonstant 2y agoisabera: he's my fiance Arch-TK: I live with her isabera: stares intensely
- izabera 2y agoHe's my fiancé :)
- prmoustache 2y agoI can still ask myself if I feel I need a different license! ;-)
- devnonymous 2y ago
- stn_za 2y agoA better use case would be to list the processes on systems without procps installed. Nice.
- c0l0 2y agoBack in 2011, I interviewed at a large-ish tech company from the US for an "SRE" role (and never had I hear that term before) that, amongst other things, created an online, browser-based alternative to MS Office. Some rounds in, after the usual phone screening, the task was some sort of supervised programming in a $thatcompany Docs document while talking to the interviewer on speaker phone. Since I had rated "shell scripting" and "Linux" fairly highly in my mandatory self-assessment sheet that I had turned in weeks prior, I was tasked with conjuring a `netstat` replacement in bash. I quickly realized I couldn't do it (because I did not, at the time, know where and how exactly socket information was held in /proc/), but I offered to write trimmed-down replacements of `ps` and `fuser` instead. The interviewer deemed that - and my solutions eventually produced in that wretched, browser-based word processor - acceptable, and a few weeks later, I shipped out to the on-site interview series. Now I wonder if the hypothetical scenario presented as the motivation for this exercise is more grounded in reality than Izabera (thanks for all your help in #bash over years btw!) would care to admit... ;)
- inferiorhuman 2y agoDollars to donuts you'll find some of these system utilities poking at procfs/sysfs in the first place. That's where I'd start anyways.
- c0l0 2y agoNecessarily, as this is the kind of API (a file system with a well-defined structure) that the kernel exposes for querying/providing that kind of information :)
- inferiorhuman 2y agoI've never dug too far beyond procfs on Linux, but always assumed there was a more formal C API backing most of these things up like with sysctl (or in general on the BSDs). Relying so heavily on string parsing seems great for quick shell scripts but less great for other languages.
- zokier 2y ago
- zoidb 2y ago> What if you're ssh'd into a machine, you're in your trusty bash shell, but unfortunately you cannot spawn any new processes because literally all other pids are taken. What do you do? A long ago for fun I created an interactive website to explore this type of scenario. https://oops.cmdchallenge.com https://oops.cmdchallenge.com
- dolmen 2y ago"echo *" doesn't list ALL files in the directory. "echo .* *" does.
- teddyh 2y agoIt does list all files if you run set -O dotglob
- gigatexal 2y agoi would begin to panic and think wtf why am i in this uber-grey-beard-dev-sys-ops interview??? im just a data engineer
- lilyball 2y agoThis is cool, but once I beat it, it just brought me back to the first step and wouldn't let me see the others. That's frustrating because I wanted to check out the "View Solutions" lists for the other steps to see what other approaches I could have taken.
- fossdd 2y ago> What if you're ssh'd into a machine, you're in your trusty bash shell, but unfortunately you cannot spawn any new processes because literally all other pids are taken. What do you do? But what if i'm instead in my trusty POSIX shell without bash support? The bash script is not POSIX complient :(
- gchamonlive 2y agoAlmost ps aux
- bheadmaster 2y ago> # turns out that the most difficult problem in computer science is aligning things > # this one function looks simple but it took so fucking long Heh. I can't count how many times I've written a column-aligning functions in various programming languages, and each time it is a pain. And it sounds simple in my head - just get max-length of each column, and add spaces up to the next multiple of tab-size. But even in Python with f-strings and all the fancy padding stuff it has, it is ends up a convoluted, unreadable mess: # randomwordgenerator.com table = [ ['agony', 'kick', 'pump'], ['frown', 'lonely', 'mutation'], ['sail', 'tasty', 'want'], ] tab_width = 4 n_rows, n_cols = len(table), len(table[0]) max_width = [ max(len(table[r][c]) for r in range(n_rows)) for c in range(n_cols) ] for r in range(n_rows): for c in range(n_cols): item = table[r][c] if c == n_cols - 1: # do not print space after last item print(item) else: # only print newline after last item # EDIT: found a bug here after commenting... # width = int(((max_width[c] + 1) / tab_width) * tab_width) width = ((max_width[c] // tab_width) + 1) * tab_width print(f'{item:<{width}}', end='') Even while writing this code for the comment for the hundredth time, I've had to fix at least 5 bugs! Truly horrible.
- freedomben 2y agoOh man, seriously! It's way harder than it seems like it would. It can also be tempting to make use of tabs, but IME that usually makes it worse because you will hit edge cases that mess it up, so then you have to start tracking different behavior. It's a hell of trek.
- anthk 2y agoawk does that for free. Heck, awk can read /proc just fine. No bash needed.
- guipsp 2y ago> without ever forking
- nknld 2y ago[dead]
- mgaunard 2y agoThat's some pretty clean bash. In my experience most bash code is badly written and inefficient, but this is a good example of code that isn't.
- Annatar 2y agoClean bash would mean that it is portable; that bash script will only work on Linux, whereas it will horribly break everywhere else.
- chasil 2y agoThis does not work in bash 3.2, but it is functional on bash 4.2. $ ./psaux.bash ./psaux.bash: line 182: printf: `(': invalid format character ./psaux.bash: line 185: printf: `(': invalid format character $ rpm -q bash bash-3.2-33.el5_11.4.0.1
- kelnos 2y agoI think it's entirely reasonable not to support a bash release series that is 18 years old on an OS release series that is 17 years old.
- protosam 2y agoYou can also write a listener and client in bash. In practice, I don’t recommend it.
- Annatar 2y ago[dead]