Managing processes in the Linux operating system can be a challenging task, but Linux provides powerful tools to simplify and enhance the process. One such useful tool is the trap command. In this article, we’ll explore what the trap
command does and how to use it with practical examples.
What is the trap
Command?
The trap
command is used to handle signals that can be sent to a process. Signals are a way for other processes or the system to notify a process of specific events or states. For instance, a signal can be sent to a process to request it to terminate or restart.
The trap
command allows us to define actions that should be taken when a process receives a specific signal. Possible actions include executing certain commands, ignoring the signal, or even terminating the process.
trap
Command Syntax
The syntax of the trap
command is as follows:
trap 'command' SIGNAL
-
'command'
– the command or list of commands to execute when theSIGNAL
is received. -
SIGNAL
– the name of the signal to respond to.
You can list the signals that the trap
command can respond to using the -l
option:
$ trap -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
Examples of Using the trap
Command
Example 1: Handling the INT Signal (Ctrl+C)
One of the most common uses of the trap
command is to handle the INT
signal (generated by pressing Ctrl+C), which interrupts the execution of a process. We can use trap
to make a process perform specific actions before termination.
#!/bin/bash cleanup() { echo "Exiting..." # Add commands to clean up resources or terminate other tasks exit 0
} trap cleanup SIGINT # Main program code
while true; do echo "Working..." sleep 1
done
In this example, when a user presses Ctrl+C, the program first executes the cleanup
function and then exits.
Example 2: Simultaneously Terminating Multiple Commands
Often, you need to run several long-running commands concurrently. While you can run each command from a separate terminal, what if you want to launch them all at once from a single script? You can use the &
symbol to run commands in the background, like this:
# Start background process 1
./background_process_1 & # Start background process 2
./background_process_2 & # Last process
./background_process_last
The above script launches ./background_process_1
and ./background_process_2
in the background, while ./background_process_last
runs in the foreground. If you press Ctrl+C, only the last command will receive the SIGINT
signal, and the previous background commands will remain running, requiring you to find and terminate them manually (e.g., using the ps
command).
This situation can be resolved easily using trap
. Example:
#!/bin/bash # Set a handler for the SIGINT signal
# and send the signal to background processes using kill
cleanup() { echo "Exiting..." kill %1 kill %2 exit 0
} trap cleanup SIGINT # Start background process 1
./background_process_1 & # Start background process 2
./background_process_2 & # Last process
./background_process_last
In this example, when you press Ctrl+C (which generates the SIGINT
signal), the signal handler executes the kill %1
and kill %2
commands, where %1
and %2
represent the identifiers of the first and second background processes (./background_process_1 &
and ./background_process_2 &
). This sends the SIGTERM
signal to these processes, allowing them to gracefully handle the termination.
This example demonstrates how to use the trap
command to handle the SIGINT
signal and manage background processes launched in the background.
Conclusion
The trap
command is a valuable tool in the Linux operating system for signal handling and process management. It empowers programmers to control the behavior of their programs when different signals are received. Knowledge of the trap
command can be beneficial for system administrators and developers working with Linux.
In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.