Signals
Signals in Linux are messages sent to active processes in order to interrupt them. By sending signals, you can stop, terminate or restart running processes. You can also move a foreground process to the background or the other way around.
Kill Command
The kill
command is used to send a signal to an existing process. Without any option, the command sends the SIGTERM
signal, which is a termination signal. For this command, you need to specify the process with the process ID or job ID. When you use the job ID, you need to put %
before the job ID. The kill command can be used for both background and foreground processes.
For example, if you want to stop one of the background processes in the example in the previous section, run the command below.
kill 85945
By running the jobs
command, you can see that the process has been terminated.
jobs -l
[1] 85945 Terminated ./loop.sh > countdown_bg.txt
[2]+ 85946 Stopped ./loop.sh > countdown_bg.txt
[3] 85947 Running ./loop.sh > countdown_bg.txt &
[4]- 85949 Running ./loop.sh > countdown_bg.txt &
Other Signals
By running the kill
command with the -l
option, you can see the available list of signals.
kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
:
For example, SIGKILL
is used to forcefully terminate a process. Sometimes, SIGTERM
may not terminate the process for some reason. When you really want to terminate the process, you can use this signal.
In the same example, send SIGKILL
and check the result with the jobs
command. You can see that the result shows that the process is killed (and not terminated).
kill -SIGKILL 85947
jobs -l
[2]+ 85946 Stopped ./loop.sh > countdown_bg.txt
[3] 85947 Killed ./loop.sh > countdown_bg.txt &
[4]- 85949 Running ./loop.sh > countdown_bg.txt &
Ctrl Z and Ctrl C
Ctrl Z and Ctrl C are also signals. These can be used for a foreground process. Ctrl Z is used to stop (suspend) a process. Ctrl C is used to terminate a process.