Terminating processes on Unix systems is not quite an art, but there are sure a lot more options for how to select and terminate Unix processes than there are ways to skin a cat. In this post, we take a quick look at some of the commands and options that are likely to be the most useful.\n\nControl sequences\n\nThe most obvious way to kill a process is probably to type Ctrl-C. This assumes, of course, that you've just started running it and that you're still on the command line with the process running in the foreground. There are other control sequence options as well. You could suspend a process by using Ctrl-Z and then running a command such a kill %1 (depending on how many background processes you have running) to snuff it out.\n\n\nCtrl-C sends SIGINT (interrupt)\nCtrl-Z sends TSTP (terminal stop)\nCtrl- sends SIGQUIT (terminate and dump core)\nCtrl-T sends SIGINFO (show information), but this sequence is not supported on all Unix systems\n\nKill commands\n\nThen, there are the various kill commands -- including kill, pkill, xkill, and killall. The xkill command is a kill command for X Windows, so I won't cover it here. The others are general purpose and provide very specific or fairly general process termination options.\n\nkill\n\nThe kill command expects to be provided with a process ID and offers options such as -9 (often referred to as the \u201csure kill\u201d option) to control what signal is sent to a running process. With the sure kill option, the kernel stops a process dead in its tracks giving it no time to execute a proper shutdown routine. Because this can result in data loss, this option should only be used when the process cannot otherwise be stopped.\n\n$ kill -9 7911\n$ kill -s 9 7911$ kill -KILL 7911\n\n\nOther commonly used kill options include:\n\n\n-1 (-HUP) is the "hang up" option. This option is very safe. Most programs are built to listen for this signal and shut down in an orderly fashion if they do.\n\n\n-2 (SIGINT) is the same signal that is used when you type Ctrl-C. It is generally safe, though some data loss is possible.\n\n\n-15 (-TERM) is the "terminate" signal and the default. Like -1 (-HUP), it tells the process to shut down and is generally safe, though -1 (-HUP) is still the gentlest and most predictable.\n\n\nYou can get a complete list of the signals available to you using the kill -l (-l for "list") command.\n\n$ kill -l\n 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP\n 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1\n11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM\n16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP\n21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ\n26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR\n31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3\n38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8\n43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13\n48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12\n53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7\n58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2\n63) SIGRTMAX-1 64) SIGRTMAX\n\npkill\n\nIn its simplest form, the pkill command is handed the name of a process and sends it a SIGTERM (just like the kill command). The benefit is that you do a little less work, not having to look up the process ID, and the command does a little more. Of course, if you're trying to kill a particular process and sixteen of them are running on the system, you'll only be able to shoot down those that belong to you -- unless you're using your superuser powers. Sometimes you want to shut down one particular process. Sometimes you want to kill by name.\n\n\nIf we want to terminate a looping process called "loopy", we can do that easily:\n\n$ pkill loopy\n\n\nWe also could have done it this way, but that would even be more work.\n\n$ ps -ef | grep loopy\nshs 4310 4168 0 13:52 pts\/0 00:00:00 \/bin\/bash .\/loopy\nshs 4314 4168 0 13:52 pts\/0 00:00:00 grep --color=auto loopy\n$ kill 4310\n\n\nWe also have the option of using the -u (username) option to kill by username, but note that this will attempt to terminate all processes being run by this user.\n\n# pkill -u shs\n\n\nAlternately, we can kill one specific process for one specific users.\n\n# pkill -u shs loopy\n\n\nWant to log off real quick and shut down everything you're doing?\n\n$ pkill -u `whoami`\n\n\nThis will kill all processes you might have running, even those running in the background.\n\n\nThe pgrep and pkill commands share a man page since they, after all, are just different sides of the same executable.\n\n\nOptions that work only for pgrep:\n\n\n-d delimiter between process IDs\n-l (include username in output)\n\n\noptions that work only for pkill\n\n\n-signal (specify signal to be sent)\n\n\nThe number of variety of signals that are available is quite mind-boggling. Under normal circumstances, however, you will probably only make use of these.\n\nSignal Name Signal Value Behaviour\nSIGHUP 1 Hangup\nSIGKILL 9 Kill Signal\nSIGTERM 15 Terminate\n\n\nYou can use the numeric or the more descriptive name in your kill commands -- kill -9 and kill -KILL have the same effect.\n\n\nBut as you saw, if you read my post on pgrep, there are numerous other options available with pkill. You can specify the user, the effective userid, the terminal, the signal to be sent, the process group, just the oldest or newest instance of a process, etc.\n\n\nsample commands\n\n$ pkill -U intruder\n$ pkill -U `whoami`\n$ pkill -n myproc\n$ pkill -f partial\n# pkill -9 -P 1984\n# pkill -g 123\n\n\nYou can also terminate a specific process by providing more information about the process you're targeting. The -f option allows you to specify more than just the process name using a command pattern like this:\n\npkill -9 -f "COMMANDNAME -PARAMETERS"\n\nkillall\n\nThe killall command also provides the kill-by-name capability and, like kill and pkill, sends a SIGTERM by default. If some process seems to be replicating itself with wild abandon, you can shut it down with a command like killall repproc, killall -9 repproc, or killall -q -9 repproc. With the -q options, the command will run "quietly" and you won't see output from the kill operation.\n\n\nOptions available with the killall command include:\n\n-e exact match\n-I ignore case\n-g process group\n-i interactive (ask for confirmation before killing)\n-l list known signal names\n-o older than\n-q work quietly\n-r interpret process name as regular expression\n-s send the specified signal\n-u specified user\n-v verbose\n-V show version\n-w wait for all processes to die\n-y younger than\n-Z specify security context\n\nWhy signals?\n\nWhenever you generate a signal using any form of kill command, the operating system interrupts the target process' normal flow of execution. What happens next depends on the signal being sent.\n\n\nThe pkill and killall commands came about to make terminating process easier by identifying processes by name and other criteria. Going back to our \u201cloopy\u201d example, you could kill the process in one fell swoop, rather than looking up the process ID and then killing it. While the effect may be the same in both cases, the end result depends on whether other processes by the same name are running and whether you have sufficient access to terminate them.\n\n\nWhat's easiest depends on what you're trying to do. Is your focus on one particular user, one particular process, or a wider range of users and processes? In general, using additional options allows your kill commands to be more targeted.