Killing a Command in Progress
If you’ve launched a command from the shell running in the foreground, and want to kill it immediately, type ^C. The shell recognizes ^C as meaning, “terminate the current foreground command right now.” So if you are displaying a very long file (say, with the cat command) and want to stop, type ^C:
$ cat bigfile
This is a very long file. It has lots of lines. It will take a very long time to output. Blah blah blah blah blah blah. By now you've probably been waiting for about a minute and a half, and decided "I don't need to do this right now" and want to stop it from continuing. ^C
$
To kill a program running in the background, you can bring it into the foreground with fg and then type ^C, or alternatively, use the kill command:
Kill [options] [process_ids]
The kill command sends a signal to the process. This can terminate a process (the default), interrupt it, suspend it, crash it, and so on. You must own the process, or be the superuser to affect it.
$ kill 13243If this does not work- some programs catch this signal without terminating- add the -KILL option:
$ kill -KILL 13243which is virtually guaranteed to work. However, this is not a clean exit for the program, which may leave resources allocated (or other inconsistencies) upon its death.
If you don’t know the PID of a process, try the pidof command:
$ /sbin/pidof emacsor run ps and examine the output.
In addition to the program, /bin/kill in the filesystem, most shells have built-in kill commands, but their syntax and behavior differ. However, they all support this usage:
$ kill -N PID
$ kill -NAME PID
where N is a signal number, and NAME is a signal name without its leading “SIG” (e.g., use -HUP to send the SIGHUP signal). To see a complete list of signals transmitted by kill, run kill -l, though its output differs depending which kill you’re running. For descriptions of the signals, run man 7 signal.
HELP! I used one of these commands, and now my shell is unusable!
In general, ^C and kill are not friendly ways to end a program. If the program has its own way of exiting, you should use that whenever possible. Both ^C and kill kill the program immediately, not giving it any chance to clean up after itself. Killing a foreground program may leave your shell in an odd or unresponsive state, perhaps not displaying the keystrokes you type. If this happens:
- Press ^J (remember, ^ = CTRL, meaning “hold down control while pressing J”) to get a shell prompt. This produces the same character as the Enter key (a newline) but will work even if Enter does not.
- Type the word reset (even if the letters don’t appear while you type) and press ^J again to run this command. This should reset your shell.
^C works only when typed into a shell. It will likely have no effect if types in a window that is not a shell window. Additionally, some programs are written to “catch” the ^C and KILL commands and ignore them: an example is the text editor, emacs.
Terminating a Shell
To terminate a shell, either run the exit command or type ^D (^D indicates “end of file” to any program reading from standard input (such as your keyboard). In this case, the program is the shell itself, which terminates).
$ exitTailoring Shell Behavior
To configure all your shells to work in a particular way, edit the files .bash_profile and .bashrc in your home directory. These files execute each time you log in (~/.bash_profile) or open a shell (~/.bashrc). They can set variables and aliases, run programs, print your horoscope, or whatever you like.
That about covers it for today. Tomorrow I’ll cover something every new Linux user needs to know: installing software!
Related posts:




