tags:

views:

145

answers:

5

How do I kill a process which is running in bash - for example, suppose I open a file:

$ gedit file.txt

is there any way within the command prompt to close it? This example is fairly trivial, since I could just close the window, but it seems to come up a bit, particularly when I mistype commands.

Also is there any way to escape an executable which is running? This probably has the same solution, but I thought I'd ask anyway.

Thanks

+4  A: 

You have a multiple options:

First, you can use kill. But you need the pid of your process, which you can get by using ps, pidof or pgrep.

ps -A  // to get the pid, can be combined with grep
-or-
pidof <name>
-or-
pgrep <name>

kill <pid>

It is possible to kill a process by just knowing the name. Use pkill or killall.

pkill <name>
-or-
killall <name>

All commands send a signal to the process. If the process hung up, it might be neccessary to send a sigkill to the process (this is signal number 9, so the following examples do the same):

pkill -9 <name>
pkill -SIGKILL <name>

You can use this option with kill and killall, too.

Read this article about controlling processes to get more informations about processes in general.

tanascius
Another option is `killall -9 <procname>`, or kill -9 ``pidof <procname>``
jweyrich
@jweyrich: Thanks - I edited my answer to reflect this.
tanascius
Be careful with `killall`. On Solaris, `killall` will kill all active processes. I would recommend using ``kill`` or ``pkill``.
David Narayan
Oh well, I assumed Linux. But you're right, the OP didn't specify it.Additionally, `pidof` utility is only standard on Linux.
jweyrich
A: 

You can use the command pkill to kill processes. If you want to "play around", you can use "pgrep", which works exactly the same but returns the process rather than killing it.

pkill has the -f parameter that allows you to match against the entire command. So for your example, you can: pkill -f "gedit file.txt"

AllenJB
+1  A: 

try kill -9 {processID}

To find the process ID you can use ps -ef | grep gedit

Liam
That -9 is a bit harsh on a normal process, you better only use that on something that does not react to a 'normal' kill request.
Simon Groenewolt
also consider using pgrep rather than grepping ps -- particularly when writing scripts
Charles Duffy
A: 

It is not clear to me what you mean by "escape an executable which is running", but ctrl-z will put a process into the background and return control to the command line. You can then use the fg command to bring the program back into the foreground.

GreenMatt
+3  A: 

You can press ctrl z. The process is sent to background and you get back to the shell prompt. Use the fg command to do the opposite.

jweyrich
I'm surprised nobody's mentioned Ctrl-Z or Ctrl-C yet. Ctrl-C harshly asks the running program to quit. Ctrl-Z should always work on a program, but some programs override Ctrl-C with a signal handler, and the signal handler might not always close the program.
Joey Adams