kill processes in one line with kill, awk, ps, and grep
This command stops the processes:
sudo ps o pgid,args | grep mininet: | sudo awk '{system("sudo kill --signal SIGSTOP -"$1)}' -
In awk
you can use system("program ")
taking the advice to use pgrep this works too:
sudo pgrep -f mininet: | sudo awk '{system("sudo kill --signal SIGCONT -"$1)}' -
Related videos on Youtube
channon
Updated on September 18, 2022Comments
-
channon over 1 year
I have a few processes that spring up and I am able to print a line of the pgid's that I would like to enter into a kill command.
here is what I have:
sudo ps o pgid,args | grep mininet: | sudo awk '{print -$0}'
returns something like
-3834 -3841 -3844 -3846 -3848 -3853 -3856 -3859 -3862
I negated the output in the
{print -$0}
part so that they kill the children processes too.the grep command searches for an argument in bash commands that denote the parent programs
now I would like to call
sudo kill -SIGSTOP
but I see here http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_9.html that you can't use commands inside the awk other than conditionals, print,etc.. Am I mistaken on this or is there a way to redirect the input to the kill command to stop the processes.context: pausing the mininet network emulator. I'd like to do this as a one-liner because it would be cool. Im sort of confused on how priority is given with
|
and how to input one command into the other.Coding by the unix philosophy I shouldnt worry about bottlenecks until later but if someone thinks that this is a bad way to do this I would appreciate that info too.
Thanks~
edit:
This command stops the processes:
sudo ps o pgid,args | grep mininet: | sudo awk '{system("sudo kill --signal SIGSTOP -"$1)}' -
In
awk
you can usesystem("program ")
taking the advice this works to:
sudo pgrep -f mininet: | sudo awk '{system("sudo kill --signal SIGCONT -"$1)}' -
-
Costas almost 9 yearsTry to use
pgrep
/pkill
-
Jakob Bennemann almost 9 years
awk
is a full programming language, so you can do many things within it that many people do not realize. However, for your case, you should almost certainly be looking intopgrep
andpkill
instead of going this route. -
channon almost 9 yearsOkay I believe the
pkill -f
flag works to search the arguments but this does not stop the child processes
-