List of common signals
Number |
Name |
Meaning |
---|---|---|
1 | SIGHUP | If a process is being run from a terminal and that terminal itself is closed/terminated then the process receives this signal and consequently terminates. |
2 | SIGINT | It politely tells the program to terminate. Performs the same function as Ctrl+C. It’s up to the process whether it will listen to it or not. |
9 | SIGKILL | Unlike other signals, the SIGKILL signal is never sent to the process. Instead, the terminal immediately kills the program and the program doesn’t get the time to save its data or clean up its work. Only use this as a last resort. |
15 | SIGTERM | This is the default signal of the kill command |
18 | SIGCONT | This will restore a process that was paused by a SIGSTOP or SIGTSTP signal |
19 | SIGSTOP | This signal pauses/freezes the process. The process cannot choose to ignore it. |
20 | SIGTSTP | It is similar to SIGSTOP but the process receiving this signal is under no obligation to listen to it. The process may choose to ignore it. |
Shell Scripting – How to send Signal to a Processes
Signals allow the operating system to communicate with programs(or processes). There are about 30 standard signals implemented by the Linux OS.
We can send signals to processes via certain keyword strokes or by the kill or wait command.
Prerequisites: Processes, Bash Scripting, Shell Function Library
Sending signals to foreground processes
Go to the terminal and start a process, say xlogo in foreground. Install xlogo if it is not present in your system.
$ sudo apt-get install x11-apps
$ xlogo
After running the process in foreground, the shell prompt will be unavailable.
- Pressing Ctrl+C sends an Interrupt signal (SIGINT) to the process and the process terminates. Restart it.
- Pressing Ctrl+Z sends a STOP signal (SIGTSTP) to the process which kind of freezes/pauses the process and the shell prompt returns.
- After pressing Ctrl+Z, you can unfreeze the paused process by moving it to foreground via fg %jobspec command or to the background using bg %jobspec command.
Sending signals to background processes
$ xlogo &
Send signals to the process via the kill command. You need the jobspec or PID (process ID) of the xlogo process running in background.
You can view the jobspec or PID using the job or ps command.
$ kill -SIGTSTP PID $ kill -SIGCONT PID $ kill -SIGINT PID
- SIGTSTP signal pauses the process
- SIGCONT signal continues the paused process
- SIGINT signal terminates the process.