The kill command is used in the shell to terminate a process. Kill works by sending a signal to the process and typically this signal is either the SIGTERM or SIGKILL signal, but there are others that can be used. To properly use the kill command you need to know the Process ID, or PID, of the process you want to kill. Also be aware that some processes can spawn child processes of the same or similar name. For instance, if you have are running the Chromium browser you may find multiple instances of the chromium process running. Killing one of these processes may not terminate all the processes because typically all but the first process are children processes. Killing any or all of the children processes will not terminate the mother process. But terminated the mother process will typically kill the children processes.
Before we delve into the kill command what is important to know is how to get the PID of a process. There are a few ways of doing this but probably the most direct is to use the ps. Full discussion of the ps command is beyond the scope of this entry and ill be covered in a future episode. That aside there are a few switches to be aware of to help you identify the PID of a process you may need to kill.
Issuing ps by itself will show you the processes for the current terminal or console session running. More than likely you will get back the shell and ps. Issuing:
Will display all the processes running on the system ordered by PID. The format will be PID, TTY, TIME and CMD where:
- PID = Process ID
- TTY = The terminal the process was started from
- TIME = Cumulative CPU Time
- CMD = The name of the command to execute the process
Another handy switch to consider is the -f which does the full-format listing:
The full-format switch will provide you with the name of the user the process is running under along with some other information.
If you are looking for a specific process to kill it may be handy to pipe the output of ps to the grep command to quickly list that process or those proccesses:
ps -ef |grep chromium
This would display all the chromium processes running
There are other ways to find the PID of a command like using top or digging through the /proc directory.
The kill requires only one parameter to be passed to it, the PID of the process to kill:
This will send the SIGTERM signal to process 1250 which will attempt to terminate the process. Signals will be discussed in a minute.
You can specify the signal to pass to the process with the -s switch:
kill -s SIGTERM 1250
kill -s 15 1250
All three versions do the same as the initial command presented above, the SIGTERM signal is sent to process 1250. With the -s switch you can pass either the signal name or the signal number. Furthermore you could just pass the number without using the -s switch like so:
kill -15 1250
Finally, you can pass the signal name without the SIG prefix like the number:
kill -TERM 1250
There are a number of signals that can be passed to a process:
- SIGHUP – 1 – Hangup – SIGHUP tells the process that the controlling terminal has closed and the process should be terminated. If the process is a daemon then usually this will cause the daemon to re-read the configuration file.
- SIGINT – 2 – Terminal Interrupt – This signal sends an interrupt to the process equivalent to pressing <ctrl>+<c>
- SIGQUIT – 3 – Terminal Quit – This is the termination signal sent to the process when the user requests the process perform a core dump. This does not necessarily make a core dump.
- SIGKILL – 9 – Kill – This signal tell the process to terminate immediately and cannot be caught or ignored and no clean-up is performed when the signal is received.
- SIGTERM – 15 – Termination – This is the default signal sent to a process by the kill command. It tell the process to terminate. This signal can be caught or ignored by the process and allows for the process to release resource and state saving where appropriate.
- SIGCONT – 18 – Continue executing if stopped – This will resume a stopped process, see SIGSTOP
- SIGSTOP – 19 – Stop executing – This will stop a process, equivalent to <ctrl>+<z> and cannot be caught or ignored. Process can then be resumed with the SIGCONT signal.
- SIGSTP – 20 – Terminal Stop Signal – Like SIGSTOP, the process is sent the SIGSTP signal to temporarily stop. The process can then be resumed with the SIGCONT signal.
There are a lot more signals than those listed above. In fact you can get a full list of the signal commands by passing the -l switch to kill:
If you want to know the name of a specific signal number:
kill -l 9
The output will be KILL
If you want to know the number of a specific signal name:
kill -l SIGTERM
The output will be 15
Consult the biblography below for more information on what these signals mean and do. Of the signals above chances are you will only use SIGTERM and SIGKILL.
How you pass the Process ID is very important. Where PID is greater than 0 kill treats that as sending a signal to the process with that PID. You can list a number of PIDs separted by a space:
kill -9 2443 2321 2981
This will send the SIGKILL signal to the processes with the PID’s listed.
If you pass 0 as the PID kill will send the signal to every process in the process group that you executed the kill command from. Doing this will effectively terminate your terminal or console session and any processes that were spawned from that session.
Passing -0 will not send a signal but report back a 0 for success and a 1 for failure. The return is not displayed but you can echo out like this:
kill -0 3299 ; echo $?
Using -1, by itself, will send the signal to every process that you have permission to kill. You do not want to do this as this will effectively terminate your entire session more than likely requiring a reboot.
A value of -n where n is greater than 1 will send the signal to every process in the process group identified by -pid.
The -p option can be used to identify the PID of a named command. This option can be a bit tricky to use as it requires the full path to kill be specified. On most systems kill will be in /bin/kill but it could also be /usr/bin/kill:
/bin/kill -p chromium
This will return the PID for the chromium processes. This may not return all the child pids for the process named. In this case if you actually did:
ps -ef |grep chromium
You would see more processes running that PID’s that would be returned by the mentioned command. But if you passed those PID’s from the -p switch to the kill command it would terminate Chromium completely.
There is a way to kill processes by command name and that is using the -a switch. Like the -p, the -a requires you to pass the full path to kill:
/bin/kill -a firefox
This will kill all processes named firefox.
BONUS BONUS BONUS
Worms is a cute little application included in the bsd-games package for most distrubtions. Worms runs from the terminal and generates little squiggly critters from ascii characters that move about the terminal. Running worms on most modern systems will produce a cacophony of crazy doodles like a grainy film stock. Therefore, you probably want to run worms with the -d switch which puts a delay in milliseconds:
worms -d 100
By default three worms will appear in the terminal squirm about. You can change this with the -n switch:
worms -d 100 -n 7
Now you will see 7 worms squirming about. The length of these worms is a chain of 16 characters by default and can be controlled with the -l switch:
worms -d 100 -n 5 -l 10
You should see 5 worms squiggling about each with 10 characters in length.
To have the worms leave a trail behind them use the -t switch:
worms -d 100 -t
Now where ever your worms squiggle to they will leave behind a “.” eventually filling up the terminal.
The final switch that worms takes is -f which creates a field for the worms to eat:
worms -d 100 -f
The terminal will fill up with the word WORM over and over and your little squigglers will eat their way randomly around the terminal leaving a blank space, or a “.” if you specified the -t switch, behind them.
To exit the worms program simply press <ctrl>+<c> or better yet, practice the kill command from above on your worms sessions!
- man kill
- man 2 kill
- man worms
- info kill
- linux signals
- Wikipedia article on Unix Signals
- Wikipedia kill command
- Linux Planet kill tips
- Oreilly on kill command
If the video is not clear enough view it off the YouTube website and select size 2 or full screen. Or download the video in Ogg Theora format:
- Episode 019 – kill and worms Ogg Theora Video – Archive.org
Thank you very much!
- Ogg file hosted by Hacker Public Radio
- Speex file hosted by Hacker Public Radio
- Mp3 file hosted by Hacker Public Radio
3 Responses to Episode 019 – Kill the worms!