Signals

What is a signal?

A signal is a message sent to a running process to notify it of a particular event. It is a form of inter-process communication (IPC), although very limited as the messages are standardized.

What happens when a process receives a signal?

The operating system (Linux) interrupts the process to deliver the signal.

What does a process do when it receives a signal?

Some signals can't be caught or ignored (like SIGKILL and SIGSTOP), but others can be handled by the process that receives them. The process can then take one of the following actions:

  • Handle the signal (do something about it)
  • Use the default handler for that particular signal
  • Ignore it

What happens after the signal is handled by the process?

If the process is not stopped or terminated by the signal, it will resume its normal execution after the signal is handled.

What kind of signals are there?

You can list all available signals by running the following command:

kill -l
What are the most common signals?

Some of the most common signals you will see are:

Signal Description
SIGHUP Used to reload a process' configuration.
SIGINT Used to interrupt a process. Sent by pressing Ctrl + C .
SIGKILL Used to terminate a process suddenly (it can't be handled or ignored).
SIGTERM Used to terminate a process gently, allowing it to clean up before finishing.
SIGSTOP Used to stop a process (it can't be handled or ignored).
SIGTSTP Used to stop a process (it can be handled or ignored). Sent by pressing Ctrl + Z .
SIGCONT Used to resume a process which has been stopped.
SIGPIPE Sent to notify the process writing to a pipe that there is no other process reading from the pipe.
How can I send a signal to a process?

Although the name might be misleading, you can send any signal to a process using the kill command.

kill usage

Description: kill sends a signal to a process.

Usage: kill [-SIGNAL] PID

Example: kill -SIGTERM 1234

This example sends the signal SIGTERM to the process with PID 1234.

Note: the SIGTERM signal is sent if no signal is specified.

There is a variant of this command called pkill, which allows you to specify a pattern to match against the process name.

pkill usage

Description: pkill sends a signal to processes matching a pattern.

Usage: pkill [-SIGNAL] PATTERN

Example: pkill -SIGUSR1 myapp

This example sends the signal SIGUSR1 to the processes matching the pattern myapp.

Note: the SIGTERM signal is sent if no signal is specified.

Can I define my own signals?

No, although there are 2 reserved signals that can be used by developers to implement custom behavior within their programs: SIGUSR1 and SIGUSR2.

Can I send signals to any process?

You can only send signals to processes you own, unless you are the root user, who can send signals to any process.

Where can I practice using signals in a real-world scenario?

You can apply what you've just learned in these free interactive scenarios:

Where can I find more info about signals?

Man pages:

man 1 kill
man 7 signal

External link: Wikipedia