Kill: The Command to End All Commands

 in
Need to get rid of a job that's gotten into a loop and refuses to end? Here's a command that will take care of the problem.
Using kill

Suppose we had inadvertently started cat in the background; Ctrl-c would be ineffective, because the signal wouldn't get to the cat process. So we need to send it a signal some other way. With the command kill, you can send any signal to any process you own. The command's syntax is:

kill -SIGNAL

If no signal is specified on the kill command line, the terminate signal SIGTERM (default) is sent. This will normally terminate the process in question. If it fails to do so—that is, SIGTERM was caught or ignored—you can send the signal SIGKILL, which will always terminate the process.

Thus, we might do the following to terminate our runaway cat process commands. First determine the PID:

$ ps
  PID  TT STAT   TIME COMMAND
 2037  p0 S      0:01 cat

Now, kill process 2037, which is the cat process:

kill 20371
If cat had been written to catch SIGTERM, we would have to use a signal that cannot be caught or ignored.
kill -SIGKILL 20371
In addition to killing errant processes, kill can be used to inform processes that the status of something has changed. For example, suppose you are writing a program and you wish to have it change its mode of operation on the occurrence of some external event. By coding what is called an “interrupt handler” in your program, you can have it catch any number of signals which have meaning to you. In particular, you might choose SIGUSR1 or SIGUSR2, which are non-specific. By sending your chosen signal, you can make your program aware of the change in circumstances, so it can proceed into its alternate mode of operation.

When you use kill, the desired signal is sent only to processes you own (that is, processes that you invoked). This prevents inadvertent termination of the wrong process. The exception is that the superuser (root) can use kill to send a signal to any process. Similarly, any process owned by root can send a signal to any other process.

An orderly shutdown of your system can occur in this way. While the kill command is not used at shutdown, the equivalent system call kill(2) is used to terminate everything. This guarantees that no files are left open and that all buffers are written to disk. For a description of kill(2), enter man 2 kill at the prompt.

A related command is killall, which takes the name of the process as an argument rather than the process ID (PID). (Some versions of kill can take process names too.) This is a convenient way to terminate all processes with the same name. If a path is used to identify the process to be signaled, only the processes executing that particular file are selected. In addition, you can ask to be consulted before killall kills a particular process, and you can receive confirmation that the signal was actually sent.

Although full details are listed in the man page, an example may be useful here. Suppose you have two programs that are different but have the same name—perhaps different release levels. In order to be different and have the same name, they must be stored in different directories. Assume they have the name sample_prog, but one is stored in /usr/a and the other in /usr/b. Entering ps gives the following output:

 PID TTY STAT  TIME COMMAND
123 pp0   S     0:03  /usr/a/sample_prog
124 pp1   R     0:02  /usr/b/sample_prog

The following commands perform different actions:

# To kill both processes
killall sample_prog
# To kill only process 123
killall /usr/a/sample_prog

Conclusion

In summary, the kill and killall commands can be useful tools to control the execution of processes on your Linux system. In combination with other tools described in previous “Take Command” columns, they will allow you to become true masters of a very powerful desktop appliance. For specific information on their very few options, and for a description of the signals they can invoke, read the relevant manual pages (enter man kill or man killall at the prompt).

Dean Provins is a professional geophysicist and licensed Amateur Radio operator (VE6CTA) in Calgary, Alberta. He has used Unix systems since the mid 1980's and Linux since January, 1993, when he read an article about it in the Calgary Unix Users' Group newsletter. Dean uses Linux as a development system for geophysical software, and as a text processing system for newsletter and other articles. He is currently developing a Linux application to view scanned images of articles published in the American Radio Relay League's monthly journal QST. He can be reached at provinsd@cuug.ab.ca.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Kill child processes

Anonymous's picture

I am trying to kill all child processes without the parent. Lets just say it is bash that is running multiple times. I want to kill all of the child processes for bash but leave the parent running. Does anyone know how I can do that. I have tried the multiple commands and they either don't work or they kill everything. Any suggestions?

Kill Commands

Anonymous's picture

I am trying to kill all child processes without the parent. Lets just say it is bash that is running multiple times. I want to kill all of the child processes for bash but leave the parent running. Does anyone know how I can do that. I have tried the multiple commands and they either don't work or they kill everything. Any suggestions?

kill by process name

Anonymous's picture

To kill by process name, use pkill -signal

pkill processname -signal

Anonymous's picture

pkill processname -signal

I always thought that kill

Anonymous's picture

I always thought that kill -SIGKILL would kill any process, until I started plugging USB drives into my machine. I keep having problems where they lockup and cannot be listed, unmounted or anything else. If I do an ls on the mounted file system, it never comes back. I can find the process id of the ls process, but kill -9 on that process id does nothing. Anyone have something stronger than kill -9?

i'm running leopard on my

olson's picture

i'm running leopard on my AlBook G4 1.5 how do you use the process name i.e. "safari" "firefox" in conjunction with kill/killall?

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix