Technical skill

Linux signals: management

Send a short message to a running program

Sometimes a program needs to be asked to stop, paused, or notified that something happened. Linux can send it a signal, a short notification that the program may respond to in a defined way. A signal does not carry a long explanation. The program may have a built-in response, install a handler, block it temporarily, or ignore it, depending on the signal.

To ask process 1234 to finish, send SIGTERM:

kill -TERM 1234

The kill command sends a signal to a process or process group; it does not always kill or terminate. Without a signal name, it normally sends SIGTERM. Check with ps -p 1234 -o pid,ppid,stat,comm that the PID still names the intended process first. SIGTERM asks the program to exit. A handler may flush data or release resources, but the cleanup depends on the program.

Know when a signal can be caught

Each signal has a default action. It may terminate, stop, continue, or ignore the process. Programs can install handlers for many signals, block them temporarily, or ignore them. SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. The program cannot run a handler when either signal is delivered.

If a program will not exit after a reasonable interval and immediate termination is appropriate, SIGKILL asks the kernel to terminate it without allowing the target to handle or ignore the request:

kill -KILL 1234

A successful send does not prove the process has already exited; it may take time for the kernel to finish termination. A failed send can mean the process exited, the PID is stale, or the caller lacks permission. kill -0 checks existence and permission without sending a signal:

kill -0 1234

That result is only a snapshot. The process can exit or its PID can be reused as soon as the check completes.

Terminal and script cleanup

Pressing Ctrl-C commonly sends SIGINT to the terminal's foreground job. SIGHUP often follows a terminal disconnection, though a program can give it other behavior. SIGSTOP suspends a process and SIGCONT resumes one. Use kill -l for names supported locally and signal(7) for default actions.

A Bash script can arrange cleanup when the shell exits or receives a catchable termination signal:

trap 'rm -f -- "$tmpfile"' EXIT HUP INT TERM

This assumes tmpfile has already been set. A trap for HUP, INT, or TERM runs its action; the shell does not exit automatically just because the action ran. Add an explicit exit if that is intended. Cleanup should tolerate being called more than once, and SIGKILL cannot run the trap.

In C, use sigaction() to set handlers and keep handler work minimal. A handler should use only async-signal-safe operations; complex cleanup belongs in ordinary program flow. A service manager or application-specific shutdown command can provide a fuller stop sequence than signaling an arbitrary child.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful