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

7,031

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)}' - 
Share:
7,031

Related videos on Youtube

channon
Author by

channon

Updated on September 18, 2022

Comments

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

    taking the advice this works to:

    sudo pgrep -f mininet: | sudo awk  '{system("sudo kill --signal SIGCONT -"$1)}' - 
    
    • Costas
      Costas almost 9 years
      Try to use pgrep / pkill
    • Jakob Bennemann
      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 into pgrep and pkill instead of going this route.
    • channon
      channon almost 9 years
      Okay I believe the pkill -f flag works to search the arguments but this does not stop the child processes