Kill processes in one line with kill, awk, ps, and grep

awkkillprocess-management

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 use system("program ")

taking the advice this works to:

sudo pgrep -f mininet: | sudo awk  '{system("sudo kill --signal SIGCONT -"$1)}' - 

Best Answer

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 Question