Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 997651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:04:06+00:00 2026-05-16T07:04:06+00:00

I often need to kill a process during programming. The way I do it

  • 0

I often need to kill a process during programming.

The way I do it now is:

[~]$ ps aux | grep 'python csp_build.py'
user    5124  1.0  0.3 214588 13852 pts/4    Sl+  11:19   0:00 python csp_build.py
user    5373  0.0  0.0   8096   960 pts/6    S+   11:20   0:00 grep python csp_build.py
[~]$ kill 5124

How can I extract the process id automatically and kill it in the same line?

Like this:

[~]$ ps aux | grep 'python csp_build.py' | kill <regex that returns the pid>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:04:07+00:00Added an answer on May 16, 2026 at 7:04 am

    In bash, using only the basic tools listed in your question(1), you should be able to do:

    kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')
    

    Details on its workings are as follows:

    • The ps gives you the list of all the processes.
    • The grep filters that based on your search string, [p] is a trick to stop you picking up the actual grep process itself.
    • The awk just gives you the second field of each line, which is the PID.
    • The $(x) construct means to execute x then take its output and put it on the command line. The output of that ps pipeline inside that construct above is the list of process IDs so you end up with a command like kill 1234 1122 7654.

    Here’s a transcript showing it in action:

    pax> sleep 3600 &
    [1] 2225
    pax> sleep 3600 &
    [2] 2226
    pax> sleep 3600 &
    [3] 2227
    pax> sleep 3600 &
    [4] 2228
    pax> sleep 3600 &
    [5] 2229
    pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
    [5]+  Terminated              sleep 3600
    [1]   Terminated              sleep 3600
    [2]   Terminated              sleep 3600
    [3]-  Terminated              sleep 3600
    [4]+  Terminated              sleep 3600
    

    and you can see it terminating all the sleepers.

    Explaining the grep '[p]ython csp_build.py' bit in a bit more detail: when you do sleep 3600 & followed by ps -ef | grep sleep, you tend to get two processes with sleep in it, the sleep 3600 and the grep sleep (because they both have sleep in them, that’s not rocket science).

    However, ps -ef | grep '[s]leep' won’t create a grep process with sleep in it, it instead creates one with the command grep '[s]leep' and here’s the tricky bit: the grep doesn’t find that one, because it’s looking for the regular expression "any character from the character class [s] (which is basically just s) followed by leep.

    In other words, it’s looking for sleep but the grep process is grep '[s]leep' which doesn’t have the text sleep in it.

    When I was shown this (by someone here on SO), I immediately started using it because

    • it’s one less process than adding | grep -v grep; and
    • it’s elegant and sneaky, a rare combination 🙂

    (1) If you’re not limited to using those basic tools, there’s a nifty pgrep command which will find processes based on certain criteria (assuming you have it available on your system, of course).

    For example, you can use pgrep sleep to output the process IDs for all sleep commands (by default, it matches the process name). If you want to match the entire command line as shown in ps, you can do something like pgrep -f 'sleep 9999'.

    As an aside, it doesn’t list itself if you do pgrep pgrep, so the tricky filter method shown above is not necessary in this case.

    You can check that the processes are the ones you’re interested in by using -a to show the full process names. You can also limit the scope to your own processes (or a specific set of users) with -u or -U. See the man page for pgrep/pkill for more options.

    Once you’re satisfied it will only show the processes you’re interested in, you can then use pkill with the same parameters to send a signal to all those processes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 491k
  • Answers 491k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I solved this by creating a method that catches the… May 16, 2026 at 10:08 am
  • Editorial Team
    Editorial Team added an answer I don't do Jetty, so I looked a bit round… May 16, 2026 at 10:08 am
  • Editorial Team
    Editorial Team added an answer Looking at the C++ docs for the windowIcon property they… May 16, 2026 at 10:08 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I often need to display information based on or influenced by a user's actual
I often need to run reduce (also called foldl / foldr, depending on your
I often need to design a dialog in Delphi/C++Builder that allows various properties of
I often need to enter text (consisting of repeated characters) like this: ------------------------------------ TODO
I often need to execute custom sql queries in django, and manually converting query
I often need to develop stuff on the road, with no internet/network connection. I
I often need to use a function which performs and action X is condition
I often need relatively small (<10000 entries <1kb) caches for speeding up calculations. My
While developing ASP.NET applications I often need to parse a boolean value given in
I'm working from inside an ipython shell and often need to reload the script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.