A useful command is the kill command, which is used to send signals directly to any process on the system. It is usually called with two parameters—the signal type and the PID. For example, if you have made changes to the configuration file for the Internet super daemon, you must send a signal to the daemon to tell it to reread its configuration file. Note that you don't need to restart the daemon itself: this is one of the advantages of a process-based operating system that facilitates interprocess communication. Ifinetd had the PID 167, typing
# kill -1 167
would force inetd to reread its configuration file and update its internal settings. The –1 parameter stands for the SIGHUP signal, which means "hang up." However, imagine a situation in which you want to switch off inetd temporarily to perform a security check. You can send a kill signal to the process by using the –9 parameter (the SIGKILL signal):
# kill -9 167
Although SIGHUP and SIGKILL are the most commonly used signals in the shell, several others are used by programmers and are defined in the signal.h header file. Another potential consequence of sending a signal to a process is that instead of "hanging up" or "being killed," the process could exit and dump a core file, which is a memory image of the process to which the message was sent. This result is useful for debugging, although too many core files will quickly fill up your file system! You can always obtain a list of available signals to the kill command by passing the –l option:
$ kill -l HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2 CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING LWP FREEZE THAW RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX
kill
Signal
|
Code
|
Action
|
Description
|
|---|---|---|---|
SIGHUP
|
1
|
Exit
|
Hangup
|
SIGINT
|
2
|
Exit
|
Interrupt
|
SIGQUIT
|
3
|
Core
|
Quit
|
SIGILL
|
4
|
Core
|
Illegal instruction
|
SIGTRAP
|
5
|
Core
|
Trace
|
SIGABRT
|
6
|
Core
|
Abort
|
SIGEMT
|
7
|
Core
|
Emulation trap
|
SIGFPE
|
8
|
Core
|
Arithmetic exception
|
SIGKILL
|
9
|
Exit
|
Killed
|
SIGBUS
|
10
|
Core
|
Bus error
|
SIGSEGV
|
11
|
Core
|
Segmentation fault
|
SIGSYS
|
12
|
Core
|
Bad system call
|
SIGPIPE
|
13
|
Exit
|
Broken pipe
|
SIGALRM
|
14
|
Exit
|
Alarm clock
|
SIGTERM
|
15
|
Exit
|
Terminate
|
pgrep
The pgrep command is used to search for a list of processes whose names match a pattern specified on the command line. The command returns a list of corresponding PIDs. This list can then be piped to another command, such as kill, to perform some action on the processes or send them a signal.
For example, to kill all processes associated with the name "java," the following command would be used:
$ kill –9 'pgrep java'
pkill
The pkill command can be used to send signals to processes that have the same name. It is a more specific version of pgrep, since it can be used only to send signals, and the list of PIDs cannot be piped to another program.
To kill all processes associated with the name "java," the following command would be used:
$ pkill –9 java
killall
The killall command is used to kill all processes running on a system. It is called by shutdown when the system is being bought to run-level 0. However, since a signal can be passed to the killall command, it is possible for a superuser to send a different signal (other than 9) to all processes. For example, to send a SIGHUP signal to all processes, the following command could be used:
# killall 1