How to kill line of PID?

5,652

Solution 1

You could pipe the output to xargs e.g.

ps -ef | grep <process_name> | awk '{print $2}' | xargs /bin/kill

But why doesn't your pkill command work?

Solution 2

With

pgrep process_name | xargs kill

or

ps -e | awk '/[p]roces_name/ {system("kill "$1}
Share:
5,652

Related videos on Youtube

rɑːdʒɑ
Author by

rɑːdʒɑ

Updated on September 18, 2022

Comments

  • rɑːdʒɑ
    rɑːdʒɑ over 1 year

    I have process which created multiple PID's. I want to kill all those PID's. I have tried
    pkill <process_name>.

    But PID not getting killed as they were wait to resource releasing.

    I have managed to get PID list with

    ps -ef | grep <process_name> | awk '{print $2}'
    

    which gives process ID list but how can I kill all those listed PIDs ?

    Thank you.

    • Dani_l
      Dani_l over 8 years
      what's the output of pgrep -lf <process_name> ? if it gives only the relevant processes, you can kill with pkill -f <process_name>
    • l0b0
      l0b0 over 8 years
    • Dani_l
      Dani_l over 8 years
      @l0b0 Why? because some web page says so? The pgrep -lf verification covers everything that page warns about. You can see exactly what you are about to pkill.
    • Dani_l
      Dani_l over 8 years
      @l0b0 how is pgrep/pkill different from ps -ef | grep <prco name>? Since it doesn't, you claim the authoritative answer is - since you didn't think to store the ppid in advance, you're out of luck. Don't trust ps names. They might lie.
    • l0b0
      l0b0 over 8 years
      Hold on, I didn't say to use pgrep/pkill instead of ps | grep; they're equally bad. If you read the linked web page it explains how to do it cleanly, by making sure that the parent process is responsible for killing/relaunching processes rather than relying on a PID file or pgrep/pkill. Of course, I assume OP is trying to automate this because of the form of the question. If that's not the case there's no point in being picky.
    • Dani_l
      Dani_l over 8 years
      @l0b0 The major issue I have with the linked solution is the complete disregard for daemons, which are always detached from the parent to init (1). Assuming that all processes are always run from from terminal is somewhat naive.
    • l0b0
      l0b0 over 8 years
      @Dani_l Daemons like (AFAIK) MariaDB and Apache httpd use a top-level process to keep track of all their sub-processes, and therefore follow this pattern. By making the top-level process a simple wrapper you can provide a single interface to keep track of all the workers, and you won't need a PID file or p* commands. If you still think this is a bad idea I suggest asking on programmers.SE.
  • Dani_l
    Dani_l over 8 years
    Or pkill -f ...