Skip to main content

Kill All Matching Processes with grep

October 29, 2021
1 min read
Terminal window
ps aux | grep python | awk '{print $2}' | xargs kill -9

Shared by Kim Ji-seong from Naver Boostcamp AI Tech 2nd cohort.

  • The pipe ( | ) passes the output of the preceding command to the next.
  • ps aux retrieves information about all running processes.
  • grep python filters for lines containing “python”.
  • awk '{print $2}' extracts only the second column, which is the PID.
    • awk blog
    • awk can select fields and records.
    • Here it prints the content of the 2nd field.
  • xargs kill -9 kills all the extracted PIDs.
    • xargs blog
    • xargs uses piped input as arguments for the given command.
    • The awk output becomes the arguments for kill -9.
Loading comments...