Linux kill command and Signal Control

Source: Internet
Author: User

As a result of your responsibilities, you have to read the notes for obscure Linux applications. Then, you run the command and edit the settings file. Everything is running and living
Beautiful. However, you know, good times will not last forever. When you encounter the frightening "Send the process a sighup" prompt, good time is over.

 
What is "sighup" and how do you send it? Is it like a bunch of flowers you send to your lover? Although you are sure this is not a command line command, you should try to type it.
Of course, there is no result. Then, check the keyboard. No sighup key. So you read the reference guide for this application again and see the following text:

When a hangup signal is received, the sshd program will re-read the configuration file. Send the sighup signal by executing the commands and options when starting the program, such as/usr/sbin/sshd.

Oh, it turned out to be.

Programmers vs. users

The online reference guide for Linux programs generally takes care of both the needs of end users and the needs of senior programmers. Therefore, some descriptions are difficult to understand. But don't worry. Now we need to unveil the mysterious veil that covers these confusing contents.

Signal and Process Control

 
This problem mainly belongs to the scope of signal and process control. For our system administrators and common users, we are mainly concerned with starting, stopping, and restarting services, stopping out-of-control processes, and pending processes.
And do not interrupt the system as much as possible. Because different operating systems and different command shell processes different signals, we will only introduce the Linux operating system and bash shell.

 
Signals are used to communicate with the daemon and process. Any active task is a process, and the daemon is a background service waiting to respond to certain events or execute tasks according to the schedule. A program must
A signal processing program built in it is used to capture and respond to signals. Signal in Linux
The Reference Guide explains various signals and their usage. The signal is sent by the "kill" command. The kill-l command displays a list of available signals and their numbers.

All daemon and processes have a process ID (PID), for example, the content displayed after the PS name is used:

$ PS aux
User PID % CPU % mem tty STAT command
Root 1 0.0 0.1? S init [2]
105 7783 0.0 0.2? SS/usr/bin/container-daemon -- System
Hal 7796 0.0 0.7? SS/usr/sbin/Hald
Postfix 7957 0.0 0.2? S qmgr-l-t fifo-u-c
Nagios 8371 0.0 0.2? SNS/usr/sbin/Nagios/etc/Nagios. cfg

 
This output is simplified. You can see more rows and columns in the system. If some processes consume all your CPU or memory, you can find it in the % CPU and % mem output column.
They. A quicker way to find out a process that is out of control is to use the top command, because the process that uses the most CPU resources is displayed at the top according to the default settings. We can use a "yes"
Command to test:

$ Yes Carla is teh awesum

This command will repeatedly display "Carla is teh awesum" at a high speed until you stop running it. This will enable your CPU usage to reach the threshold.

$ Top
...
PID user PR Ni virt res shr s % CPU % mem time + command
12144 Carla 25 0 31592 17 m 13 M r 93.4 0: 50. 26 konsole
22236 Carla 15 0 2860 468 S 400 4.3. 97 Yes

 
After analyzing this result, you will find some interesting things. You will find that the program that consumes the most CPU is the konsole virtual terminal program, rather than the "yes" command. This is because "yes"
The command is run in the konsole terminal program. If you run the same command sequence in a "real" Console (Press CTRL + ALT + F2), you will see that the "yes" command is ranked
First.

There are many ways to stop the "yes" command. If you want to return to the shell that runs it, press Ctrl + C. Alternatively, you can use the "kill" command in another shell to stop running the "yes" command. The "kill" command is followed by the PID or command name, as shown below:

$ Killed 22236

Or

$ Killall Yes

 
Press Ctrl + C to send a SIGINT (Signal 2), which is an interrupt signal that the keyboard requires control. Both kill and killall commands are issued according to the default settings.
A sigterm signal (number 15 ). In the program, the sigterm signal (15) can be captured or ignored, or interpreted in different ways. Therefore, if your program
The response of the kill command is different from your expectation. It may be a problem with the target program to be killed.

Terminating a parent process usually terminates its child process. However, this is not always the case. Do you know what a sub-process is? Add the-F option to the ps command, as shown below:

$ PS axf
22371? R :35 _ konsole [kdeinit]
22372 pts/3 ss | _/bin/bash
24322 pts/3 S + | _ Yes Carla is teh awesum
22381 pts/4 Rs | _/bin/bash
24323 pts/4 R + | _ PS axf

Now, go back to the sighup topic

Sighup is pronounced as "sig-hup", abbreviated as signal hangup, meaning "stop signal ". How do you send a sighup signal? There are several methods:

# Kill-HUP [pid]

# Killall-hup [process-name]

# Kill-1 [pid]

# Killall-1 [process-name]

 
Therefore, you can use the PID or name, signal name or number. So why not use the/etc/init. d/Foo command to restart the system? Use their own
Init (initialization) files to control the service are preferred because these files usually contain sound and error checks and additional features. The main reason for using the "kill" command and signal is to try
Stop pending and out-of-control processes explicitly without restarting or logging out.

Terminate a process

As you can see in the man page of the signal, there are more than a dozen ways to control the process. Below are some common methods:

Kill-stop [pid]

Send sigstop (17,19, 23) to stop a process without killing it.

Kill-cont [pid]

Send sigcont (, 25) to start a stopped process again.

Kill-kill [pid]

Send sigkill (9) to force the process to stop immediately without cleaning.

Kill-9-1

Terminate all processes you have.

The sigkill and sigstop signals cannot be captured, blocked, or ignored. However, other signals are acceptable. So this is your weapon.

BASH Shell Kil command l

The bash shell contains a built-in kill command. When you execute the following command:

$ Type-all kill
Kill is a shell built-in
Kill is/bin/kill

The command results show that there are two kill commands, one is the built-in bash command, and the other is the/bin/kill executable program. Generally, these two commands may not conflict with each other. However, if you encounter a kill command exception, you can specify the/bin/kill command explicitly.

You must refer to the reference resources listed in the following resources to learn more about kill in Linux, because this is your ticket to enter the field of Linux system maintenance. This knowledge allows you to maintain the system like a surgical procedure, instead of restarting the system every time you encounter a problem, as we know about some of the poor operating systems.

Resources

Chapter 7 "start and end Linux" in Linux cookbook"

Bash (1)-GNU Bourne-again shell

Yes (1)-print characters repeatedly before termination

Signal (7)-available signal list

PS (1)-report the snapshot of the current process

Kill (1)-sends a signal to a process

Killall (1)-Kill processes by name

Pkill (1)-view or send a process Signal Based on the name and other attributes

Skill (1)-send a signal or report the Process status

Xkill (1)-destroy a customer program based on X Resources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.