Previous Table of Contents Next


Module 70
kill

DESCRIPTION

The external and internal kill command is used to send signals to processes currently executing on the system. Its name is derived from the fact that most signals will cause a process to die, thus it kills the process. Normally kill is used in conjunction with the ps command, system's, or the PID or ? variable.

COMMAND FORMAT

Following is the general format of the kill command.

     kill [ -signal ] PID ...
     kill -signal -PGID ...
     kill -l

BSD (Berkeley)
kill [ -signal ] PID ...
kill -l

C shell & Korn shell
The C shell and Korn shell have additional formats for the internal kill command if job control is supported on the system.
kill -l
kill [ -signal ] [ PID... ]
kill [ -signal ] [ %n... ]

Options

The following options may be used to control how kill functions.

-signal Specifies the signal sent to the given PIDs. You can use numbers or the corresponding names. Refer to the following table.

C shell & Korn shell
-l Displays (lists) the signal numbers and names.
%n Specifies a job number to kill.

If no signal is specified, signal 15 is used. Signal 15 is the signal for software termination, which causes the program to perform internal cleanup, if it is written to do so, and then exit.

Arguments

The following arguments may be passed to the kill command.

PID ... One or more Process IDs you wish to send a signal.
PGID ... One or more Group Process IDs you wish to send a signal. All processes with the PGID group process ID are sent the signal.
0 If the specified PID is zero, then the signal is sent to your login shell and all processes started by it. For example,
                         kill 0
sends a software terminate signal to all of your processes, even your shell. The shell catches and ignores the signal. But most other commands are terminated.

FURTHER DISCUSSION

The kill command is useful for killing runaway or hung background processes. A runaway process is often caused by an infinite loop in a program. In any case the kill command can be used to remove a process from the system. A hung process may be caused by a disconnected device, such as a terminal, or printer that has hung. If a background process is waiting for input that will never arrive, you will need to kill the process.


NOTE:  
Because of the way UNIX handles process signaling, it is possible to have a process hang and not be able to kill it. This is caused by the program not being able to process its signal queue. Thus when you send a signal to the process to terminate it, the process never actually receives the signal. In these situations the system administrator may have to reboot the system to remove the process.



The following table lists the signals you can send to a process. Use the -l option to list out available signals and their corresponding names on your system. The symbolic names and numbers are not the same on all systems. The symbolic names in parenthesis are aliases.


Signal
Number
Symbolic
Name
Description

1 HUP Hang-up. The phone line disconnected.
2 INT Interrupt. Pressed the Delete key.
3 QUIT Quit. Pressed the Ctrl-\ key.
4 ILL Illegal instruction
5 TRAP Trace trap
6 IOT(ABRT) IOT instruction
7 EMT EMT instruction
8 FPE Floating point exception
9 KILL Kill process. Cannot be trapped
10 BUS Bus error
11 SEGV Segmentation violation
12 SYS Bad argument to system call
13 PIPE Write on a pipe with no one to read it
14 ALRM Alarm clock
15 TERM Software termination signal (default signal sent from kill)
16 USR1 User-defined signal 1
17 USR2 User-defined signal 2
18 CLD(CHLD) Death of a child
19 PWR Power fail restart
20 WINCH Window or mouse signal
21 URG Urgent socket condition
22 POLL(IO) Selectable event pending (asynchronous i/o)
23 STOP Stop process
24 TSTP Stop process requested from tty (keyboard)
25 CONT Continue after a stop
26 TTIN Background process requested tty (keyboard) input; stops process
27 TTOU Background process requested tty (terminal) output; stops process
28 VTALRM Virtual timer alarm
29 PROF Profiling timer alarm
30 XCPU Exceeded cpu limit
31 XFSZ Exceeded file size limit


NOTE:  
For a list of signals handled by your system use the kill -l command. If your system does not support the -l option, then try using the command "grep SIG /usr/include/*/signal.h." Remember to strip off the SIG prefix.



RELATED COMMANDS

Refer to the ps command described in Module 109.

APPLICATIONS

The kill command is used to terminate runaway or hung processes that have been placed in the background. It can also be used from another terminal to terminate an interactive process that has become inoperable for some reason.

When killing application programs it is usually a good idea to terminate the process with a -15 signal first. If the process does not die within a reasonable amount of time (10 to 60 seconds), then you may have to repeat the kill command or send a -9 signal.

TYPICAL OPERATION

In this activity you use the kill command to terminate a background command. Begin at the shell prompt.

1.  Type the following lines; press Return after each line.
       cj> while true
       do
        echo "An infinite loop!"
        sleep 5
       done &
       3781

C Shell
       cj> while ( 1 )
         echo "An infinite loop!"
         sleep 5
       end &
       3781


This creates an infinite loop that prints a message to your screen, then sleeps for five seconds and repeats itself. Notice that a number was returned to your screen after you pressed Return on the last line. This is the Process ID (PID) of the background process.
2.  To terminate the process type kill 3781 and press Return. Replace 3781 with the number returned after the line ending with &.
If you started a subshell sh, type exit and press Return to return to your csh.
3.  Turn to Module 94 to continue the learning sequence.


Previous Table of Contents Next