One Linux command per day: Kill command

Source: Internet
Author: User

The KILL command in Linux is used to terminate the run of the specified process (terminate a process) and is a common command for process management under Linux. Typically, terminating a foreground process can use the CTRL + C key, but for a background process to be terminated with the KILL command, we need to use a tool such as ps/pidof/pstree/top to get the process PID, and then use the KILL command to kill the process. The KILL command ends the process by sending a specified signal to the process. By default, a term signal of number 15 is used. The term signal terminates all processes that cannot capture the signal. For those processes that can capture the signal, a kill signal numbered 9 is used to forcibly "kill" the process.

1. Command format :

kill[parameter [process number]

2. Command function :

Sends the specified signal to the corresponding process. Not specifying a model will send Sigterm (15) to terminate the specified process. If the program can not be terminated with the "-kill" parameter, it sends a signal of Sigkill (9), which will force the end of the process, using the PS command or the jobs command to view the process number. The root user will affect the user's process, and non-root users can only affect their own processes.

3. Command Parameters :

-L signal, if no signal number parameter, then use "-l" parameter will list all the signal name

-A does not restrict the correspondence between the command name and the process number when processing the current process

-p Specifies that the KILL command prints only the process number of the related process without sending any signals

-s specifies the signal to send

-U Specify user

Note :

1. Kill command can be with signal number option or without. If there is no signal number, the KILL command emits a stop signal (15), which can be captured by the process, allowing the process to clean up and release resources before exiting. You can also use kill to send a specific signal to the process. For example:

Kill-2 123

The effect is equivalent to pressing the CTRL + C key while the foreground is running the PID 123 process. However, a normal user can only use a KILL command without the signal parameter or a maximum of 9 signals.

2. Kill can have a process ID number as a parameter. When you send a signal to these processes with kill, you must be the owner of those processes. If you attempt to revoke a process that does not have permission to revoke or undo a process that does not exist, you get an error message.

3, you can signal to multiple processes or terminate them.

4. When kill successfully sends a signal, the shell displays the process termination information on the screen. Sometimes this information is not displayed immediately, only if you press ENTER to make the shell's command prompt reappear.

5, it should be noted that the signal to force the process to terminate, which often brings some side effects, such as data loss or terminal can not return to normal state. Be careful when sending a signal, only use the Kill signal (9) only if it is a last resort, because the process cannot capture it first. To undo all background jobs, you can enter kill 0. Because some commands that run in the background start multiple processes, it's a hassle to track down and find the PID of all the processes you want to kill. At this point, it is a valid method to use Kill zero to terminate all processes initiated by the current shell.

4. Usage examples :

Example 1: List all signal names

Command :

Kill-l

Output :

[[email protected] test6]# kill-l 1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill 5) SIGTRAP 6) Sigab     RT 7) Sigbus 8) SIGFPE 9) SIGKILL) SIGUSR1) SIGSEGV) SIGUSR213) sigpipe     (SIGTERM) SIGSTKFLT17) (SIGCHLD) sigcont) SIGSTOP) SIGTSTP21) sigttin  Sigurg) SIGXCPU25) sigxfsz) sigvtalrm sigprof) SIGWINCH29) SIGIO) SIGPWR 31)  Sigsys) SIGRTMIN35) (sigrtmin+1) sigrtmin+2 (PNS) sigrtmin+3) sigrtmin+439) sigrtmin+5) sigrtmin+6 41) sigrtmin+7) sigrtmin+843) sigrtmin+9 (sigrtmin+10) sigrtmin+11) sigrtmin+1247) sigrtmin+13) SIGRTMIN+14 (sigrtmin+15) SIGRTMAX-1451) SIGRTMAX-13 SIGRTMAX-12) SIGRTMAX-11) SIGRTMAX-1055) SIGRTMAX-9 -8) (SIGRTMAX-7) SIGRTMAX-659) SIGRTMAX-5 SIGRTMAX-4) SIGRTMAX-3) SIGRTMAX-263) SIGRTMAX-1 Ax

Description :

Only the 9th signal (SIGKILL) can terminate the process unconditionally, other signal processes have the right to ignore. The following are commonly used signals:

HUP 1 terminal disconnection

INT 2 Interrupt (with Ctrl + C)

Quit 3 exit (with Ctrl + \)

Term 15 termination

Kill 9 Forced termination

CONT 18 Continuation (contrary to stop, FG/BG command)

Stop 19 paused (with Ctrl + Z)

Example 2: Get the value of the specified signal

Command :

Output :

[[email protected] test6]# kill-l kill9[[email protected] test6]# kill-l sigkill9[[email protected] test6]# kill-l term 15[[email protected] test6]# kill-l sigterm15[[email protected] test6]#

Description :

Example 3: First use PS to find the process, then kill with Kill

Command :

Kill 3268

Output :

[Email protected] test6]# ps-ef|grep vim root      3268  2884  0 16:21 pts/1    00:00:00 vim INSTALL.LOGROOT
    3370  2822  0 16:21 pts/0    00:00:00 grep vim[[email protected] test6]# kill 3268 [[email protected] test6]# Kill 3268-bash:kill: (3268)-No that process [[email protected] test6]#

Description :

Example 4: Kill the process completely

Command :

Kill–9 3268

Output :

[Email protected] test6]# ps-ef|grep vim root      3268  2884  0 16:21 pts/1    00:00:00 vim install.logroot      3370  2822  0 16:21 pts/0    00:00:00 grep vim[[email protected] test6]# kill–9 3268 [[email protected] Test6 ]# kill 3268-bash:kill: (3268)-No that process [[email protected] test6]#

Description

Example 5: Kill all processes for a specified user

Command :

Kill-9 $ (ps-ef | grep peidalinux)

Kill-u Peidalinux

Output :

[[email protected] ~]# kill-9 $ (ps-ef | grep peidalinux) [[email protected] ~]# kill-u peidalinux

Description

Method One, filter out the Hnlinux user process and kill

Instance 6:init process is not to be killed

Command :

Kill-9 1

Output :

[Email protected] ~]# ps-ef|grep initroot         1     0  0 Nov02?        00:00:00 init [3]                  root     17563 17534  0 17:37 pts/1    00:00:00 grep init[[email protected] ~]# kill-9 1[[ema Il protected] ~]# kill-hup 1[[email protected] ~]# ps-ef|grep initroot         1     0  0 Nov02?        00:00:00 init [3]                  root     17565 17534  0 17:38 pts/1    00:00:00 grep init[[email protected] ~]# Kill-kill 1 [Email protected] ~]# ps-ef|grep initroot         1     0  0 Nov02?        00:00:00 init [3]                  root     17567 17534  0 17:38 pts/1    00:00:00 grep init[[email protected] ~]#

Description

Init is one of the most indispensable programs in Linux system operation. The so-called Init process, which is a user-level process initiated by the kernel. After the kernel has booted itself (already loaded into memory, started running, and has initialized all device drivers and data structures, etc.), the boot process is completed by starting a user-level program init. As a result, Init is always the first process (its process number is always 1). All other processes are descendants of the INIT process. The init process is not to be killed!

One Linux command per day: Kill command

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.