Table of Contents
基础用法
进阶用法
kill的注意事项
Home Operation and Maintenance Linux Operation and Maintenance How to use the kill process command in Linux

How to use the kill process command in Linux

Jan 04, 2023 pm 02:21 PM
linux

在linux中,kill命令是用来杀死系统中的进程,使用步骤:1、利用ps、top、pgrep等命令获取指定应用的进程号;2、使用kill命令根据进程号杀死该进程,语法“kill  -15  进程号”或“ kill -9 进程号”。执行原理:kill命令会向操作系统内核发送一个信号(多是终止信号)和目标进程的PID号,然后系统内核根据收到的信号类型,对指定进程进行相应的操作。

How to use the kill process command in Linux

本教程操作环境:linux7.3系统、Dell G3电脑。

在Linux的系统中,kill是我们最常见的命令之一。

kill,英语中为杀死的意思,顾名思义,就是用来杀死一些东西的命令,在linux中就是用来杀死系统中的进程。

kill 命令的执行原理是这样的,kill 命令会向操作系统内核发送一个信号(多是终止信号)和目标进程的 PID,然后系统内核根据收到的信号类型,对指定进程进行相应的操作。

在Windows系统中,如果应用程序无反应我们会启动任务管理器终止应用,而在Linux系统中则使用kill命令,kill命令主要用于强制关闭进程,下面就介绍一下Linux中kill命令的基础用法和进阶用法。

基础用法


杀死后台进程的步骤:

一般情况下,想要杀死后台一个进程,我们通常只需要做两步:

第一步:找到某个应用的进程号:

ps -aux | grep 应用名称 

或 ps -ef | grep 应用名称 

或者 top  | grep 应用名称
Copy after login

How to use the kill process command in Linux

第二步:杀死进程

  • 正常杀死进程:

kill  -15  pid号
Copy after login

如杀死上图中pid为10的进程:kill -15 10

  • 强制杀死进程:

 kill -9 pid号
Copy after login

注:杀死进程的时候,推荐是正常杀死进程,而不是强制杀死进程。

原理解读:Kill命令和信号

  当你执行一个“kill”命令,你实际上发送了一个信号给系统,告诉它去终结不正常的应用。总共有60个你可以使用的信号,但是基本上你只需要知道SIGTERM(15)(正常杀死信号)和SIGKILL(9)(强制杀死信号)。

  你可以用这个命令看到所有信号的列表:

  kill -l
Copy after login

How to use the kill process command in Linux

上图:共有64中信号,每种信号均有名称和对应的信号序列号,当想要发送什么信号给系统的时候,只要告知系统该信号的序列号即可。如想要强制结束进程,则需要发送9号信号给系统,应该是这样的:kill -9 pid号。

实际中常用的只有9种信号(最常用的只有2中:9 和 15):

  • 1 终端断线

  • 2 中断(等同 Ctrl + C)

  • 3 退出(同 Ctrl + \)

  • 15 终止(可以使得进程在退出之前清理并释放资源)

  • 9 强制终止

  • 18 继续(与19相反)

  • 19 暂停(等同 Ctrl + Z)

进阶用法


1 查找进程号的方式进行改进

以查找firefox进程为例:

常规:ps -aux | grep java

进阶:pgrep java

或: pidof firefox-bin (不推荐)

解读:

  • pgrep: 这个命令是专门用于进程查询的grep。

  • pidof: 看到pidof想到啥?没错pid of xx,字面翻译过来就是 xx的PID。和pgrep相比稍显不足的是,pidof必须给出进程的全名。

2 将常规的两步杀死进程合并为一步

进阶1:

kill -s 9 `ps -aux | grep firefox | awk '{print $2}'`
Copy after login

其中awk '{print $2}' 的作用就是打印(print)出第二列的内容。根据常规篇,可以知道ps输出的第二列正好是PID。就把进程相应的PID通过xargs传递给kill作参数,杀掉对应的进程。

进阶2:

pgrep firefox | xargs kill -s 9
Copy after login

xargs kill -s 9 ”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -s 9”命令的参数,并执行该命令。“kill -s 9”会强行杀掉指定进程。

进阶3:

kill -s 9  `pgrep firefox`
Copy after login

进阶4:

pkill -9 firefox
Copy after login

前面三个进阶虽然将查找进程和删除进程合并为一个步骤,但是,仍然是两个命令,这里采用pkill命令将查找和杀掉进程的两个命令合并为一个命令了,即:pkill=pgrep + kill,表示找到并杀死进程。

进阶5:

killall -9 firefox
Copy after login

killall和pkill是相似的,不过如果给出的进程名不完整,killall会报错。pkill或者pgrep只要给出进程名的一部分就可以终止进程。

3 强制踢掉登陆用户

有的时候,可能我们的系统中有很多用户在同时登陆这一台服务器,而我们想要踢掉某个不良用户,就可以执行如下操作。

(1)查看用户登陆信息: who

(2)查看自己的身份(避免把自己踢掉):whoami

(3)踢掉用户ats

pkill -kill -t pts/2(按终端踢,pts/2为所踢用户的终端)
 或
 pkill -u ats(按用户名踢,ats为用户名)
Copy after login

kill的注意事项


1.如果kill时,不指定信号就会默认发送信号15,终止指定进程,使得进程在退出之前清理并释放资源。

2.使用kill时,root用户将影响用户的进程,非root用户只能影响自己的进程。

3.使用kill时,当kill向进程发送信号,必须是这些进程的主人。如果杀死一个没有权限杀死的进程或杀死一个不存在的进程,就会报错。如下:

-bash: kill: (20) - No such process
Copy after login

4.使用kill时,如果成功地发送了信号,shell会在屏幕上显示出进程的终止信息。(按下Enter键,就会显示出来)

5.使用kill时,如果使用信号9,使进程强行终止,可能会使数据丢失或者终端无法恢复到正常状态。

6.init进程,它是一个由内核启动的用户级进程,所有进程都是init进程的子孙,init不可杀。

相关推荐:《Linux视频教程

The above is the detailed content of How to use the kill process command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

See all articles