Linux Common Instructions---kill | Killall (Terminate process)

Source: Internet
Author: User

Kill

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

Attention:

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) SIGABRT 7) Sigbus 8) SIGFPE

9) SIGKILL) SIGUSR1 SIGSEGV) SIGUSR2

Sigpipe) sigalrm SIGTERM) Sigstkflt

SIGCHLD) Sigcont SIGSTOP) SIGTSTP

) (Sigttin) Sigttou () Sigurg) sigxcpu

SIGXFSZ) sigvtalrm sigprof) sigwinch

SIGIO) SIGPWR Sigsys) sigrtmin

(sigrtmin+1) (sigrtmin+2) sigrtmin+3) sigrtmin+4

sigrtmin+5) sigrtmin+6 sigrtmin+7) sigrtmin+8

sigrtmin+9) (sigrtmin+10) sigrtmin+11) sigrtmin+12

sigrtmin+13) (sigrtmin+14) sigrtmin+15) SIGRTMAX-14

SIGRTMAX-13) SIGRTMAX-12-SIGRTMAX-11) SIGRTMAX-10

SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7) SIGRTMAX-6

SIGRTMAX-5) SIGRTMAX-4 () SIGRTMAX-3) SIGRTMAX-2

SIGRTMAX-1) Sigrtmax

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 Kill

9[[email protected] test6]# kill-l SIGKILL

9[[email protected] test6]# kill-l term

15[[email protected] test6]# kill-l SIGTERM

15[[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.log

Root 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.log

Root 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 Init

Root 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

[Email protected] ~]# Kill-hup 1

[[email protected] ~]# PS-EF|GREP Init

Root 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 Init

Root 1 0 0 Nov02? 00:00:00 Init [3]

Root 17567 17534 0 17:38 pts/1 00:00:00 grep init

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!

Linux Common Instructions---kill | Killall (Terminate process)

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.