Kill a process with a single command

Many times we need to kill a process through a terminal. If we know the full name of the process (for example: kate) we have no problems, a simple:

killall kate

It solves the problem for us ... but what happens if we don't know the exact name of the process?

On those occasions, we have to list all processes with ps to as shown in the following screenshot:


Then look for the PID of the process, which in this case we look for the PID of kate:

By then do a:

kill 3808

And voila, there we kill the process.

Well ... in a single line we can search for the process (without needing to know its full name), find out its PID, and also kill it:
ps ax | grep kat | grep -v grep | awk '{print $2}' | xargs kill

As you can see:

  1. We list the processes (ps to)
  2. We do not know the full or exact name of kate (hey, it could be kate-editor or something like that) so we only filter by cat (grep kat)
  3. But we will get two processes related to kat if we only use this filter, one that is the kate process, and another that is the process that we activate for filtering, I leave you a screenshot so that you can finish understanding: (Notice that there are 2 lines, that is, 2 processes)
  4. To avoid what was explained before, we make another filter (grep -v grep). What we will do the opposite ... if we filter using grep, it will only show the matches with the filter, well with grep -v We instruct you NOT to show the matches, but to show what does not match. I show you the screenshot of how the result would be so far: (Note that now only kate's process appears)
  5. Well, we already have the process we want to kill isolated, now we only have to extract its PID, which is the 2nd number, that is, 4062. And the PID is in the 2nd column (1st column contains user with UID 1000), so using awk we can say that it only shows from that line what it finds in the 2nd column (awk '{print $ 2}'). Which would only show us the process number, that is, only the PID will appear in the terminal.
  6. But we do not want to show the PID, what we want is to kill the process with that PID ... so we will do that, we pass what we have so far to the command kill and ready (xargs kill)
  7. What does that xargs mean? ... simple, in this case we cannot pass the PID to kill only with pipes ( | ), this is simply not enough, so xargs (that allows to pass values ​​or data and then execute or kill them) is what will allow us to finish the job.

And here it ends 😀

Yes ... I know this seems a bit complex, which is precisely why I have tried to explain it as best I could.

I know that possibly few will need this command, but the objective of this article is the same as that of DesdeLinux, to teach them something new every day, always trying to make them lose their fear or fear of Linux ... and, personally, I would also love for them to learn to use the terminal without fear 😉

Anyway ... I hope you have found it interesting, I keep learning how to use awk which is really great hehe.

regards


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

34 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   ezitoc said

    It's true, awk has been very useful to me and I think that anyone who needs to manipulate structured text files is highly recommended to know how to use it.

    I just have a question (which has nothing to do with the input: D), how (and with what program) did you make that blur effect that allows you to highlight a portion of the screenshot?

    Greetings.

    1.    ezitoc said

      Testing if this from the format it works and if not someone tell me how to do it

      Thank you very much.

    2.    KZKG ^ Gaara said

      Well yes ... I have rediscovered Linux now that I know how to work with awk HAHAHAHA.
      About the effect and such, nothing ... it's just Gimp 😀

      I select the portion that I want to highlight, cut it with [Ctrl] + [X] and paste it as a new layer, then I select the lower layer (which is the one I want to opaque) and go to Filters- »Gaussian (or whatever you write hehe) and voila.
      Now, to give it the dark effect, I simply create a new layer (white background) and place it between these two that I already had, I give it black color and in the transparency bar (upper right corner) I move it to where I achieve the desired effect .

      Greetings and thanks for the comment 🙂

      1.    Roberto Evolving Santana said

        Big!!

  2.   Manual of the Source said

    If the process is from a program that is visible, nothing is more comfortable than typing xkill on the console, click on the program to kill, and voila.

    1.    KZKG ^ Gaara said

      click on the program to kill
      hehe yeah ... that's assuming you have a GUI hehe.

      1.    Manual of the Source said

        That's right, that's why I said "if the process is from a program that is visible."

        1.    Windousian said

          It is easier to click on the button with the "X". GNOME Shell still has that button right? :-D.

          1.    Manual of the Source said

            If the program is frozen (which is the main reason why you would need to kill its process) it is logical that this button will not respond no matter how much you press it.

            I think GNOME Shell will remove it soon so you can see the tactile wonder of closing windows by dragging them to the bottom of the screen like in Windows 8. Needless to say, on widescreen monitors it's a fantastic exercise.

          2.    Windousian said

            I understand now. In that case I prefer Control + Alt + Esc (in KDE).

            I can't wait to see the new GNOME Shell previews, they are a real treat.

  3.   proper said

    This does the same but less is written.
    In this case I took leafpad as an example that is why leaf appears in grep
    ps -e | grep leaf | awk '{print $1}' | xargs kill

    regards

  4.   Sys said

    Phew! My son, try running "pgrep kat", which is "pgrep" for something.

    And to execute "man pgrep". And "man pidof", which sometimes "pidof" can help you.

    And to execute «ps aux | grep [k] at ", which will not return as a result" the process that we activate for the filtering "that you comment, thus saving you work.

    Regards!

    1.    Sys said

      Oh, and "pkill", which does what you are looking for. For example: "pkill kat".

    2.    KZKG ^ Gaara said

      Oh, interesting ... I didn't know pgrep 🙂
      Thanks for the tip 😀

      1.    Sys said

        Thanks to you and your articles.

        By the way, in https://flossblog.wordpress.com/2009/11/11/truco-del-dia-excluir-al-proceso-grep-en-la-salida-de-ps-aux/ comment on the technique of using commands like «ps aux | grep [n] program_name ", they explain it better than me there.

        Regards!

        1.    KZKG ^ Gaara said

          Thanks for the link 😀
          That's the great thing that DesdeLinux exists ... it doesn't matter if you are a user, editor or admin, we always all learn new things 🙂

          Greetings and thanks again friend.

  5.   cost said

    Thank you all very much for your time and dedication, it makes it worth visiting and reading this site several times a day.

    Thanks again.

  6.   Mr Linux. said

    KZKG ^ Gaara is almost always the same, when it comes to this kind of tips, there is another person who does the same with a simple command. But I congratulate him, he is always continuously contributing.

    1.    KZKG ^ Gaara said

      hehe yep… I know how to do X osa and I come here and share the method, but then they share a simpler way to achieve the same thing hahaha, but with this we all win, right? 😀

      1.    truko22 said

        That's right 0 /

      2.    Manual of the Source said

        Hahahaha, you always go the most complicated way. 😀

        1.    KZKG ^ Gaara said

          HAHAHA yes, I have always thought: «If I know how to do it the difficult way, then I will learn to do it the simple way without problems.»And… vice versa doesn't work the same hahaha.

  7.   Oscar said

    The problem would be if we have two processes with a similar name.
    For example, a process of kate, and another process of ... mmm ... let's say kater xD
    With such a command, we would kill both of them, right?

    1.    KZKG ^ Gaara said

      Well yeah, that would happen 🙂

  8.   truko22 said

    TT poor Kate. I use xkill in KDE it is launched quickly with "ctrl + alt + esc" or also with "ctrl + Esc" open "System Activities" and do it graphically. Now this procedure through the terminal must be learned, although I have a home server with stable debian and that does not hang at all.

  9.   Yulian said

    Great! right now that I am taking the operating systems course and I need to perform tasks with the terminal, your tutorial was a great help! thanks

  10.   Paul said

    Very well explained, great the blog I just met, I point it to favorites. Thank you.

  11.   Anon said

    Well, it's good, although there are some times when you can't kill….

  12.   Dcoy said

    pkill -9

    1.    Dcoy said

      pkill -9 "process name"
      in the previous comment I put «» but it didn't come out xD

  13.   itobest said

    good evening, I have time reading your feed and today I decided to try this command ps ax | grep chrome | grep -v grep | awk '{print $ 1}' | xargs kill and I run into the following kill error: cannot find process "?" with the little experience I have in bash I decided to make some modifications and in the end I am left with ps -A | grep c | grep -v grep | awk '{print $ 1}' | xargs kill since ps -A is used to show all processes in a summarized way and the second anomaly was that it threw me the TTY «?» and it worked for me thank you very much I really like your blog, greetings

  14.   Michael said

    Thanks compa, you can't imagine the amount of problems that you just solved for me with this command.

    Regards!!

  15.   email said

    THANKS !!!!

  16.   Arthur said

    Excellent post. Just what I was looking for and did not know how to do it, and the explanation was very good.

    Greetings.