Linux kill usage, killall, pkill, and xkill

Source: Internet
Author: User
Tags pkill

Linux kill usage, killall, pkill, and xkill

Linux kill usage, killall, pkill, and xkill

1. Kill, killall, pkill, and xkill tools for terminating Processes

Terminate a process or terminate a running program, usually through kill, killall, pkill, xkill, etc. For example, if a program is dead but cannot exit, you should consider using these tools.

In addition, in server management, you can use these tools to stop the parent process that does not involve database server programs. Why cannot the parent process of the database server be killed using these tools? The reason is simple. When these tools force terminate the database server, more file fragments will be generated for the database. When the fragmentation reaches a certain level, the database may crash. For example, it is best to close the MySQL server according to its normal program, instead of using pkill mysqld or killall mysqld as dangerous actions. Of course, we should use kill to kill database sub-processes that occupy too much resources.

1. Kill

Kill applications are used together with PS or pgrep commands;

Kill usage:

Kill [signal code] process ID

Note: The signal code can be omitted. the commonly used signal code is-9, indicating forced termination;

Example:

[Root @ localhost ~] # Ps auxf | grep httpd

Root 4939 0.0 0.0 5160 708 pts/3 S + \ _ grep httpd

Root 4830 0.1 1.3 24232 10272? SS/usr/sbin/httpd

Apache 4833 0.0 0.6 24364 4932? S \ _/usr/sbin/httpd

Apache 4834 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd

Apache 4835 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd

Apache 4836 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd

Apache 4840 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Check the httpd server process. You can also use pgrep-l httpd to view the process;

Let's take a look at the second column in the above example, that is, the PID column of the process, where 4830 is the parent process of the httpd server, and all processes from 4833-4840 are their 4830 sub-processes; if we kill the parent process 4830, the child process under it will also die;

[Root @ localhost ~] # Kill 4840 Note: Kill the 4840 process;

[Root @ localhost ~] # Ps-auxf | grep httpd Note: What are the results? Is the httpd server still running?

[Root @ localhost ~] # Kill 4830 Note: Kill the httpd parent process;

[Root @ localhost ~] # Ps-Aux | grep httpd Note: Check whether other httpd sub-processes exist and whether the httpd server is still running?

For zombie processes, kill-9 can be used to force termination and exit;

For example, if a program has completely died and kill fails to exit without signal strength, the best way is to add signal strength-9, followed by killing the parent process; for example;

[Root @ localhost ~] # Ps aux | grep Gaim

Beinan 5031 9.0 2.3 104996 17484? S Gaim

Root 5036 0.0 0.0 5160 724 pts/3 S + grep Gaim

Or [root @ localhost ~] # Pgrep-l Gaim

5031 Gaim

[Root @ localhost ~] # Kill-9 5031

2. killall

Killall uses the program name to directly kill all processes. Let's just talk about it.

Usage: name of the program that is running killall

Killall can also be used with PS or pgrep for convenience. You can use ps or pgrep to check which programs are running;

Example:

[Root @ localhost beinan] # pgrep-l Gaim

2979 Gaim

[Root @ localhost beinan] # killall Gaim

3. pkill

The pkill method is similar to the killall method. It also directly kills the running program. If you want to kill a single process, use kill to kill it.

Application Method:

# Pkill name of the running program

Example:

[Root @ localhost beinan] # pgrep-l Gaim

2979 Gaim

[Root @ localhost beinan] # pkill Gaim

4. xkill

Xkill is a program used on the desktop to kill the graphic interface. For example, when Firefox crashes and cannot exit, you can click the mouse to kill Firefox. When xkill is running and the icon of the personal brain is displayed, it will be okay if any graphics program crashes. If you want to terminate xkill, right-click to cancel it;

Xkill call method:

[Root @ localhost ~] # Xkill

Ii. Linux kill command and Signal Control

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 have a signal processing program built in it for capturing and responding to signals. The signal Reference Guide in Linux 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 them in the output % CPU and % mem columns. 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 the "yes" command to test it:

$ 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 the "yes" 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. The kill and killall commands send a sigterm signal (number 15) according to the default settings ). In the program, the sigterm signal (15) can be captured or ignored, or interpreted in different ways. Therefore, if your program's response to 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 signalhangup, 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? Using their own Init (initialization) files to control the service is a preferred method, because these files usually contain sound and error checks and additional features. The main reason for using the "kill" command and signal is to terminate the suspended and out-of-control processes as clearly as possible 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.