13.3 Terminating Processes
13.3.2 Signals and the kill command
There are currently 30-40 signals defined in UNIX depending on the implementation. Each signal is associated with a number and a name. Signals are used to terminate, suspend, and continue processes. Information on the different signals can be found by using the following man command:

$ man -s 5 signal or man signal

Using Ctrl-c can sometimes terminate a process that is not responding. This sends an interrupt (INT) signal to the process, terminating it and any child processes it might have spawned.

The
kill Command
The
kill command provides a direct way to terminate unwanted command processes. It is useful when you want to stop a command that takes a long time to run, or when you need to terminate a process that you cannot quit in the normal way. Specifying their process id normally kills processes. Background processes (those submitted using the ampersand) are also assigned a job id. You can kill background processes by using either the process id (ps) or the job id. To view all jobs running in the background and their job IDs you use the jobs command. The number returned in brackets is the job ID number

Command Format:
kill [-signal] process-id or %job-id

To terminate a process with the kill command, you would type ps to find out the PID(s) for the process(es) and then type kill followed by the PID(s). If you use the kill command without specifying a signal, signal 15 (SIGTERM) is sent to the process with the specified PID number. This is referred to as a soft kill and usually causes the process to terminate. It is best to soft kill a process if possible since it closes files properly and terminates the process(es) gracefully.

If you need to forcibly terminate a process, you can append the -9 option to the kill command. This option is referred to as a sure kill and is necessary for killing shells which will not respond to any other signal to terminate.

Command Format:
$ kill -9 PID#

Note - For processes other than shells, use the kill -9 (SIGKILL) command as a last resort because it is an abrupt method and does not allow for proper process termination.

Finding and Terminating a Process by User
The
ps command can be used with the -u (user) option to find processes for a specific user. You may find processes for users by their login name or UID number. A user can only terminate his or her own processes, but the superuser can terminate any process running on the system. If there are multiple windows open on a user's desktop, the output of the ps command will show pts/# under the TTY heading for every window open. A pts (pseudo terminal) is the device name given to windows and remote login sessions. Each window the user opens after logging in gets a new pts#. Figure shows the results of the ps -u command for user2 and uses the kill command to terminate the find process, which is PID 12932.

Command Format:
ps -u login-ID or UID

The pkill command.
The
pkill command is specific to Solaris and works exactly like the pgrep command, except that it terminates the process by matching process or processes command name (CMD) and sending a kill signal. Figure shows using the pkill command to kill a process by its name: sleep.

Command Format:
$ pkill CMD name
Interactive Media Activity (Flash, 95 kB)
  Terminating Processes

In this media activity, you are logged in as user2 and your current working directory is: /home/user2. Type the commands that would accomplish the requested objectives.
Note: Be sure to press enter after each one. Click on step 1 to begin.

Lab Activity (Adobe Acrobat Reader, 64 kB)
  Managing System Processes

In this lab, you will work with UNIX commands to identify system processes and control them.