101

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

wyatt
  • 3,188
  • 10
  • 36
  • 48
  • This looks more like a shell usage question than a shell programming question -- best practices are different between scripts and interactive usage, and the latter belongs on superuser rather than SO. – Charles Duffy May 05 '10 at 16:46
  • I don't see how this should be a duplicate of the linked question. This question asks about killing the process that is running in the current terminal. The "duplicate" question asks about finding process id of some arbitrary process for killing. – luator Feb 07 '17 at 14:34

8 Answers8

96

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
  • 53,078
  • 22
  • 114
  • 136
  • 2
    Be careful with `killall`. On Solaris, `killall` will kill all active processes. I would recommend using ``kill`` or ``pkill``. – David Narayan May 05 '10 at 16:43
  • Oh well, I assumed Linux. But you're right, the OP didn't specify it. Additionally, `pidof` utility is only standard on Linux. – jweyrich May 05 '10 at 16:58
  • 1
    +1,I used to use ps -e | grep to find the pid of the process then kill it with 'kill',which is not so wise...But _You can use this option with kill and killall,too_ ?how to **kill** with only name? – Gnijuohz Feb 23 '12 at 01:13
  • @Gnijuohz: the option I was talking about is *-SIGKILL*. To kill a process by name you better use *pkill* – tanascius Feb 23 '12 at 11:19
91

To interrupt it, you can try pressing ctrl c to send a SIGINT. If it doesn't stop it, you may try to kill it using kill -9 <pid>, which sends a SIGKILL. The latter can't be ignored/intercepted by the process itself (the one being killed).

To move the active process to background, 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
  • 31,198
  • 5
  • 66
  • 97
  • 7
    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 May 05 '10 at 16:49
  • 4
    After pausing the process with ctrl z, you can kill it with `kill -9 %`. – luator Sep 03 '15 at 09:18
  • 1
    One more thing to add: if the process does not react to Ctrl+C (which sends SIGINT), you can be a bit more brutal and use Ctrl+\ to send a SIGQUIT. – luator Feb 07 '17 at 14:37
  • isn't the keyboard shortcut "Ctrl+C" more appropriate than "Ctrl+Z" to "kill a process in bash"? – computingfreak Mar 16 '17 at 06:01
  • Sometimes CTRL+C doesn't immediately work. I just tried it with Rails, which kept doing what it was doing. Pausing and killing with `kill -9 %` worked like a charm! – Qasim Jun 01 '17 at 07:11
10

try kill -9 {processID}

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

Liam
  • 7,762
  • 4
  • 26
  • 27
5

Old post, but I just ran into a very similar problem. After some experimenting, I found that you can do this with a single command:

kill $(ps aux | grep <process_name> | grep -v "grep" | cut -d " " -f2)

In OP's case, <process_name> would be "gedit file.txt".

Ian
  • 71
  • 1
  • 5
  • 4
    Does not work although it looks good. But this works: ps -ef | grep yourproc | awk '{print $2}' | xargs kill – Timo Mar 08 '15 at 19:19
  • 2
    The separator is not good. You shoud adjust it before. this should work : kill -9 $(ps aux | grep "worker-manager-rsync" | grep -v "grep" |tr -s " "| cut -d " " -f 2) – ikken Jun 22 '17 at 15:30
  • works! i only change the last digit kill $(ps aux | grep | grep -v "grep" | cut -d " " -f3) – El David Jul 11 '17 at 15:48
  • Cut is useless for this. It does not understand that multiple spaces are one delimiter. No concept of words. – Tuntable Nov 15 '22 at 08:51
4

This one is violent use with caution :

pkill -9 -e -f processname 

If your process name is "sh" it will also kill "bash"

Yannuth
  • 500
  • 5
  • 7
1

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,245
  • 1
  • 9
  • 18
0

Please check "top" command then if your script or any are running please note 'PID'

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1384 root      20   0  514m  32m 2188 S  0.3  5.4  55:09.88 example
14490 root      20   0 15140 1216  920 R  0.3  0.2   0:00.02 example2



kill <you process ID>

Example : kill 1384
slfan
  • 8,950
  • 115
  • 65
  • 78
ganesh
  • 61
  • 6
0

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
  • 18,244
  • 7
  • 53
  • 79