this post was submitted on 20 Sep 2024
289 points (97.1% liked)

Programmer Humor

32497 readers
503 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
289
submitted 2 months ago* (last edited 1 month ago) by bi_tux@lemmy.world to c/programmerhumor@lemmy.ml
 

This happend to me right noww as I tried to write a gui task manager for the GNU/Linux OS

you are viewing a single comment's thread
view the rest of the comments
[–] bi_tux@lemmy.world 6 points 2 months ago* (last edited 2 months ago) (2 children)

probably the later, but idk how, all I did was insert a string in the following command like this:

``Command::new("bash")

.arg("-c") .arg(format!("ps -aux | grep -i "{}" | awk '{{print $2}}' | xagrs kill -9", input)

.output()

.expect("error");``

I've tested the command and it worked flawlessly in the terminal, but I have no idea what I'm doing, since I'm new to rust and never worked with this library

[–] CameronDev@programming.dev 6 points 2 months ago (1 children)

There are rust libraries to send signals, might be better to use those rather than calling bash. eg. https://docs.rs/nix/latest/nix/sys/signal/index.html

I'm guessing if input was "", then it would sigkill all processes? Less confident, but some functions behave slightly differently in an interactive console vs a non interactive, maybe ps has a different format when used non interactively?

Aside, you want three backticks and a newline to get code formatting :)
[–] bi_tux@lemmy.world 5 points 1 month ago* (last edited 1 month ago)

thx, btw I figured it out:

I forgot to trimm the string, so it had a line break in it which lead to grep showing the processes from the term I put in + all processes that contain a space/linebreak and appearently all processes shown by ps aux contain some kind of space (makes sense, since there are spaces between the user, pid, etc) so yeah, I ended up trying to kill every process on the system, but it only killed the user processes, since I ran everything without sudo