3

I would like to know what is this statement doing exactly, especially the awk part.

ps aux | grep 'sidekiq 5' | grep -v grep | awk '{ print $2; }' | xargs kill -${2:-'TERM'}

I know the entire pipeline is supposed to kill sidekiq processes.

3
  • Are you asking about the entire pipeline or the awk part only? May 7, 2021 at 9:59
  • I would like to understand the entire pipeline specially the awk part. May 7, 2021 at 10:08
  • Note: pkill may be able to replace this whole pipeline. May 7, 2021 at 12:23

1 Answer 1

10
  • ps aux lists all processes.
  • grep 'sidekiq 5' only displays those lines from the list that contain the string "sidekiq 5".
  • grep -v grep removes those lines that contain the string "grep". This is done so that the grep process from the previous pipeline stage is filtered out.
  • awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output.
  • xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command. The signal is either $2 or, if $2 is not defined, SIGTERM.
2
  • 3
    Using grep '[s]idekiq 5' will make grep -v grep unnecessary, because the [s]idekiq 5 expression matches sidekiq 5 but doesn't match the expression itself. It's an old trick.
    – gronostaj
    May 7, 2021 at 13:06
  • Yes, one encounters this trick often. Thanks for pointing it out. Slightly advanced; beginners should first study the longer but easier to understand pipeline. May 7, 2021 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .