Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the linux kill command

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "use of linux kill command", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of linux kill command" bar!

Due to the requirements of your duties, you have to struggle to read the documentation of obscure Linux applications that make you feel puzzled. Then you will run the instructions and edit the settings file. Everything is working normally, and life is really good. But, you know, the good times don't last forever. When you encounter a frightening person

When the fearful "send the process a SIGHUP" prompts, the good times are over.

What is "SIGHUP" and how do you send it? Does it look like a bouquet of flowers you give to your lover? Although you can be sure that this is not a command-line instruction, you still try to type it. Of course, it didn't work out. Then, you check the keyboard. Oh, there is no SIGHUP key. So you re-read the application's reference guide and see the following text:

When a hangup signal is received, the sshd program rereads the configuration file. Send SIGHUP signals by executing the commands and options that start the program, such as: / usr/sbin/sshd.

Oh, that's it.

Programmer VS user

The authors of online reference guides for LINUX programs generally take care of both the needs of end users and the needs of advanced programmers. Therefore, some of the instructions are difficult to understand. But don't worry. Now we need to unveil the mystery that covers these confusing content.

Signal and process control

This problem mainly belongs to the category of signal and process control. For us system administrators and ordinary users, our main concern is to start, stop and restart services, stop runaway and suspended processes, and keep the system running as uninterrupted as possible. Because different operating systems and different command shells handle signals differently, we only introduce the Linux operating system and the bash shell here.

Signals are used to communicate with daemons and processes. Any active task is a process, and a daemon is a background service that waits to react to certain events or execute tasks on schedule. A program must have a signal processor built into it to capture and respond to signals. The signal reference guide in LINUX explains the various signals and their uses. The signal is sent by the "kill" command. The kill-l command displays a list of available signals and their numbers.

All daemons and processes have a process ID (PID), such as what is displayed using ps naming:

$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/dbus-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/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 them in the% CPU and% MEM columns of this output. A quicker way to find runaway processes is to use the top command, because by default, the processes that consume the most CPU resources are displayed at the top. We can test it with a "yes" command:

$yes carla is teh awesum

This command will display "carla is teh awesum" repeatedly at a high speed until you stop it. This will bring your CPU usage to a warning level.

$top

...

PID USER PR NI VIRT RES SHR S CPU MEM TIME+ COMMAND

12144 carla 25 0 31592 17m 13m R 93.4 3.5 0:50.26 konsole

22236 carla 15 0 2860 468 400 S 4.3 0.1 0:00.97 yes

If you analyze this result, you will find something interesting. You will find that the programs that take up the most CPU are konsole virtual terminal programs, not the "yes" command, because the "yes" command runs 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 comes first.

There are many ways to stop the "yes" command from running. If you want to go back to the shell that is running it, just press the CTRL+c key. Or you can stop the "yes" command in another shell with the "kill" command, with the Kill command followed by PID or the command name, as follows:

$kill 22236

Or

$killall yes

Press the CTRL+ c key to send a SIGINT (signal 2), which is an interrupt signal that the keyboard asks for control. Both the kill and killall commands emit a SIGTERM signal (number 15) by default. The program can set whether the SIGTERM signal (15) is captured or ignored, or interpreted in a different way. Therefore, if your program reacts differently to the KILL command than you expect, it is likely to be the problem of the target program being KILL.

Terminating a parent process usually also terminates its child process. However, this is not always the case. Do you know what a child process is? You can see this using the ps command with the-f option, as shown below:

$ps axf

22371? R 2:35 _ konsole [kdeinit]

22372 pts/3 Ss 0:00 | _ / bin/bash

24322 pts/3 S+ 0:00 | | _ yes carla is teh awesum

22381 pts/4 Rs 0:00 | _ / bin/bash

24323 pts/4 R+ 0:00 | | _ ps axf

Now, back to the topic of SIGHUP.

SIGHUP is pronounced "sig-hup", which is the abbreviation of signal hangup and means "stop signal". How do you send a SIGHUP signal? Here are several ways:

# kill-HUP [pid]

# killall-HUP [process-name]

# kill-1 [pid]

# killall-1 [process-name]

Therefore, you can use PID or name, signal name or number. So why do this instead of restarting with the / etc/init.d/foo command? Using their own init (initialization) files to control services is the preferred way, because these files usually contain sound and error checking as well as additional functionality. The main reason for using "kill" commands and signals is to terminate suspended and runaway processes as explicitly as possible without having to restart or log out.

Terminate the process

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

Kill-STOP [pid]

Send SIGSTOP to stop a process without destroying it.

Kill-CONT [pid]

Send SIGCONT (19, 18, 25) to restart a stopped process.

Kill-KILL [pid]

Sending a SIGKILL (9) forces the process to stop immediately and does not perform a cleanup operation.

Kill-9-1

Terminate all processes you have.

SIGKILL and SIGSTOP signals cannot be captured, blocked, or ignored, but other signals can. So this is your ultimate weapon.

Bash shell's 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 result of the command shows that there are two kill commands, one is the built-in command for BASH, and the other is the / bin/kill executable. Generally speaking, the two commands are unlikely to encounter a conflict, but if you do encounter abnormal behavior of the kill command, you can explicitly specify the / bin/kill command.

You must take a closer look at the references listed in the resources below to learn about the wonderful use of kill in Linux, as this is your ticket to the maintenance of Linux systems. This knowledge allows you to maintain the system like a surgical operation without having to restart the system every time you encounter a problem, like some crappy operating systems we know.

Resources

Chapter 7 of Linux Cookbook, "starting and ending Linux."

Bash (1)-GNU Bourne-Again Shell

Yes (1)-print characters repeatedly before being terminated

Signal (7)-list of available signals

Ps (1)-reports a snapshot of the current process

Kill (1)-signals to a process

Killall (1)-destroy the process by name

Pkill (1)-View or signal processes based on name and other attributes

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

Xkill (1)-destroy a client program according to X resources

Thank you for your reading, the above is the content of "the use of linux kill command", after the study of this article, I believe you have a deeper understanding of the use of linux kill command, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report